HTML Form Validation
What is Form Validation?
Form validation means checking that the user filled in a form correctly.
For example, a name field must not be empty, and an email address must contain an @ sign.
HTML can do these checks for you.
You add attributes to the input fields, and the browser checks the values when the user clicks Submit.
If a value is wrong, the browser stops the form, marks the field, and shows a short message.
You do not need any JavaScript.
Required Fields
The required attribute means that the field must be filled in:
Example
<form action="/action_page.php">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" required>
<input type="submit" value="Submit">
</form>
Try it Yourself »
If you click Submit with an empty field, the browser displays a message like this:
Please fill out this field.
Note: Each browser writes its own messages. The text can be different in your browser and in your language.
Validation by Input Type
Some input types check the value by themselves.
A type="email" field only accepts text that looks like an email address:
Example
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
Try it Yourself »
If you type "hello" and click Submit, the browser displays a message like this:
Please include an '@' in the email address. 'hello' is missing an '@'.
The type="url" and
type="number" also check the value by themselves.
Tip: Learn about all input types in the HTML Input Types chapter.
Numbers: min and max
The min and max attributes set the smallest and the largest number the user can enter:
Example
<label for="age">Age (18 to 99):</label>
<input type="number" id="age" name="age" min="18" max="99" required>
Try it Yourself »
If you type 12 and click Submit, the browser displays a message like this:
Value must be greater than or equal to 18.
Tip: The min and max attributes also work with dates and times.
Text Length: minlength and maxlength
The minlength and maxlength attributes set how many characters the text can have:
Example
<label for="username">Username (4 to 12 characters):</label>
<input type="text" id="username" name="username" minlength="4" maxlength="12" required>
Try it Yourself »
If you type "abc" and click Submit, the browser displays a message like this:
Please lengthen this text to 4 characters or more (you are currently using 3 characters).
The maxlength attribute works in another way.
It does not show a message, it simply stops the user from typing more than 12 characters.
Patterns
The pattern attribute checks the value against a regular expression (a short code that describes what the text must look like).
Use the title attribute to tell the user what you expect:
Example
A product code must be three capital letters followed by three digits:
<label for="code">Product code:</label>
<input type="text" id="code" name="code"
pattern="[A-Z]{3}[0-9]{3}"
title="Three capital letters and three digits, like ABC123" required>
Try it Yourself »
If you type "abc12" and click Submit, the browser displays a message like this, often with the text from title under it:
Please match the requested format.
Three capital letters and three digits, like ABC123
Tip: Learn more about regular expressions in our JavaScript tutorial.
Validation Attributes
| Attribute | What it checks |
|---|---|
| required | The field is not empty |
| type | The value fits the type, like email, url, or number |
| min and max | The number or date is not too small or too large |
| minlength and maxlength | The text is not too short or too long |
| pattern | The text matches a regular expression |
| step | The number fits the step, like whole numbers only |
Style Valid and Invalid Fields
CSS can show the user which fields are wrong.
The CSS :user-invalid pseudo-class styles a field with a wrong value, and
:user-valid styles a field with a correct value:
Example
input:user-invalid {
border: 2px solid red;
}
input:user-valid {
border: 2px solid green;
}
Try it Yourself »
Tip: These styles only apply after the user has changed the field, or tried to submit the form.
Tip: Do not use color alone to show an error. Some users cannot see the difference between red and green, so also use a clear border, an icon, or a text message.
Turn Off Validation
The novalidate attribute on the <form> element turns off all browser checks:
Example
<form action="/action_page.php" novalidate>
This is useful when you want to write your own checks with JavaScript.
Always Check on the Server Too
Browser validation helps users fix mistakes quickly. However: It does not protect your website!
A user can turn off the checks, change the HTML, or send data to the server without using your form at all.
The server must always check the data again before it uses it.
Summary
- The browser checks form fields when the user clicks Submit
requiredmakes a field mandatory- Input types like
emailandnumbercheck the format min,max,minlength,maxlength, andpatternset the rules:user-invalidand:user-validstyle the fields- Always check the data on the server too