Lesson 2.5 β Form Validation Basics
π Preventing Incorrect Data Entry in HTML Forms
π― Learning Objectives
After completing this lesson, students will be able to:
β Understand Form Validation
β Understand why validation is important
β Use Required Fields
β Restrict Input Length
β Validate Email Addresses
β Validate Numbers
β Use Placeholder Text
β Create Professional Forms
π Introduction
Imagine a College Admission Form.
Student enters:
Name: _________
Email: abc
Mobile: 123
Is this valid information?
β No
Problems:
- Name is empty
- Email is incorrect
- Mobile number is too short
To solve this problem websites use:
β Form Validation
Validation checks whether the entered information is correct before submission.
π€ What is Form Validation?
Form Validation is the process of checking user input before accepting it.
Real Life Examples
Registration Form
Checks:
- Username entered?
- Password entered?
Admission Form
Checks:
- Student Name
- Mobile Number
Login Form
Checks:
- Username
- Password
π Why Validation is Important?
Without Validation:
β Wrong Data
β Fake Information
β Empty Fields
β System Errors
With Validation:
β Correct Information
β Better User Experience
β Professional Forms
π― Required Attribute
The most common validation.
Makes a field mandatory.
Syntax
<input type="text" required>
Example
<label>Name:</label>
<input
type="text"
required>
Result
If empty:
Browser displays an error.
Form cannot be submitted.
Example Program
<form>
<label>Name:</label>
<input
type="text"
required>
</form>
Output
Student must enter a name.
π Placeholder Attribute
Provides guidance to users.
Example
<input
type="text"
placeholder="Enter Your Name">
Output
[Enter Your Name]
appears inside the field.
Why Use Placeholder?
Helps users understand:
What information should be entered.
Example
<input
type="email"
placeholder="student@gmail.com">
π― Email Validation
Using:
type="email"
automatically validates email format.
Example
<input
type="email"
required>
Valid Email
student@gmail.com
β Accepted
Invalid Email
studentgmail.com
β Rejected
Example Program
<label>Email:</label>
<input
type="email"
required>
π Number Validation
Using:
type="number"
Example
<input
type="number">
Output
Only numbers allowed.
Example
<label>Age:</label>
<input
type="number"
min="18"
max="60">
Meaning
Allowed Age:
18 to 60
π― Min Attribute
Defines minimum value.
Example
<input
type="number"
min="1">
Result
Cannot enter value below 1.
π― Max Attribute
Defines maximum value.
Example
<input
type="number"
max="100">
Result
Cannot exceed 100.
Example
<input
type="number"
min="1"
max="100">
π Minlength Attribute
Defines minimum character length.
Example
<input
type="text"
minlength="3">
Valid
Rahul
Invalid
Ra
Too short.
π Maxlength Attribute
Defines maximum character length.
Example
<input
type="text"
maxlength="20">
Result
Maximum 20 characters.
Example
<label>Name:</label>
<input
type="text"
minlength="3"
maxlength="30">
π― Pattern Attribute
Used for custom validation.
Example
Mobile Number Validation
<input
type="tel"
pattern="[0-9]{10}">
Meaning
Exactly 10 digits
Required.
Valid
9876543210
β Accepted
Invalid
12345
β Rejected
π Readonly Attribute
User can view data but cannot modify it.
Example
<input
type="text"
value="Computer Science"
readonly>
Output
Student can see it.
Cannot edit it.
π Disabled Attribute
Completely disables field.
Example
<input
type="text"
value="Disabled"
disabled>
Output
Greyed field.
Cannot be used.
π― Complete Validation Example
<!DOCTYPE html>
<html>
<head>
<title>Validation Example</title>
</head>
<body>
<h1>Student Registration Form</h1>
<form>
<label>Name:</label>
<br>
<input
type="text"
required
minlength="3"
maxlength="30">
<br><br>
<label>Email:</label>
<br>
<input
type="email"
required>
<br><br>
<label>Age:</label>
<br>
<input
type="number"
min="18"
max="60">
<br><br>
<label>Mobile Number:</label>
<br>
<input
type="tel"
pattern="[0-9]{10}"
required>
<br><br>
<input
type="submit">
</form>
</body>
</html>
π― Expected Output
Form with:
β Required Name
β Valid Email
β Age Limit
β 10 Digit Mobile Number
π« Real World Example β College Admission Form
<form>
<label>Student Name:</label>
<input
type="text"
required>
<br><br>
<label>Email:</label>
<input
type="email"
required>
<br><br>
<label>Mobile Number:</label>
<input
type="tel"
pattern="[0-9]{10}"
required>
</form>
π Validation Flow
Student Enters Data
β
Browser Checks Rules
β
Valid Data?
β
Yes β Submit Form
β
No β Show Error
β οΈ Common Beginner Mistakes
Mistake 1
Not Using Required Fields
Wrong:
<input type="text">
Better:
<input
type="text"
required>
Mistake 2
Using Text Instead of Email
Wrong:
<input
type="text">
Correct:
<input
type="email">
Mistake 3
No Mobile Validation
Wrong:
<input
type="tel">
Better:
<input
type="tel"
pattern="[0-9]{10}">
π Best Practices
β Use required fields.
β Validate email addresses.
β Validate mobile numbers.
β Use placeholders.
β Restrict input length.
β Use min and max values.
π Mini Project β Validated Registration Form
Create:
Student Name
Required
Required
Mobile Number
10 Digits
Age
18β60
Branch
Dropdown Menu
π Practical Activity
Create a form containing:
β Name
β Email
β Mobile Number
β Age
β Address
Apply:
- required
- minlength
- maxlength
- min
- max
Run it in Chrome Browser.
π Assignment
Assignment 2.5
- What is Form Validation?
- Why is validation important?
- What does the required attribute do?
- What is minlength?
- What is maxlength?
- What is the purpose of min and max?
- Create a validated registration form.
π§ Quiz
Q1. Which attribute makes a field mandatory?
A. validate
B. must
C. required
D. compulsory
β Answer: C
Q2. Which input type validates email format?
A. text
B. mail
C. email
D. url
β Answer: C
Q3. Which attribute sets minimum characters?
A. min
B. minlength
C. max
D. maxlength
β Answer: B
Q4. Which attribute sets maximum characters?
A. maxlength
B. minlength
C. max
D. size
β Answer: A
Q5. Which attribute is used for custom validation?
A. validate
B. pattern
C. check
D. rule
β Answer: B
π Lesson Summary
β Form Validation ensures correct user input.
β
required makes fields mandatory.
β
email validates email addresses.
β
min and max restrict numeric values.
β
minlength and maxlength control text length.
β
pattern creates custom validation rules.
β Validation improves accuracy and professionalism.