- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Form Validation
HTML Forms
HTML Form Validation
HTML can check form fields before they are sent: is the email field filled, does it look like an email, is the age between 18 and 60? A few attributes, and the browser shows the error message and stops the form, with no JavaScript.
This is a convenience for the user, not security. Anyone can turn browser checks off, so the server must always check the data again. The attributes themselves are described in HTML Input Attributes, and the overview of forms in HTML form basics.
The required Attribute
The field must not be empty.
Required Example
html
<form>
<label>Email:</label>
<input type="email" name="email" required>
<input type="submit" value="Submit">
</form>Try to submit it empty and the browser shows a message and puts the cursor in the field. Because the type is email, it also checks that the value looks like an email address.
The pattern Attribute
The value must match a regular expression.
Pattern Example
html
<form>
<label>Username (letters only):</label>
<input type="text" name="username" pattern="[A-Za-z]+" required>
<input type="submit" value="Submit">
</form>[A-Za-z]+ allows only letters, at least one. Always add a title that explains the rule; the browser includes it in the error message.
The min and max Attributes
The lowest and highest allowed values for numbers and dates.
Min and Max Example
html
<form>
<label>Age (18–60):</label>
<input type="number" name="age" min="18" max="60" required>
<input type="submit" value="Submit">
</form>The step Attribute
Which values are valid. With step="5", 10 is fine and 12 is not.
Step Example
html
<form>
<label>Enter number (increments of 5):</label>
<input type="number" name="quantity" step="5">
<input type="submit">
</form>The type Attribute and Validation
Some input types check the format by themselves: email needs an @, url needs a full address with https://, number accepts only numbers.
Type Example
html
<form>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<label>Website:</label>
<input type="url" name="website" required><br><br>
<input type="submit">
</form>The novalidate Attribute (Disable Validation)
Switches all browser checks off for this form. Useful while testing, or when your own JavaScript shows the messages.
Novalidate Example
html
<form novalidate>
<input type="email" name="email" placeholder="Enter email">
<input type="submit">
</form>Using title to Display a Custom Message
The title text is added to the browser's message for a pattern mismatch, so the user knows what is expected.
Custom Message Example
html
<form>
<label>Username:</label>
<input type="text" name="username" pattern="[A-Za-z]+" title="Only letters are allowed">
<input type="submit">
</form>Complete Form Validation
Example
html
<form action="/samples/form_processor.php" method="post">
<label>Full Name:</label>
<input type="text" name="fullname" required><br><br>
<label>Email:</label>
<input type="email" name="email" required><br><br>
<label>Age (18–60):</label>
<input type="number" name="age" min="18" max="60" required><br><br>
<label>Password:</label>
<input type="password" name="password" minlength="6" maxlength="12" required><br><br>
<label>Website:</label>
<input type="url" name="website" required><br><br>
<input type="submit" value="Submit Form">
</form>Leave a field empty, type a wrong email, or enter an age of 15, and the form refuses to submit, pointing at the first problem.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
Good to know
- Browsers show the errors when the user presses Submit. To style fields only after the user has interacted with them, use the CSS
:user-invalidpseudo-class. - Error messages appear in the browser's language and style, which you cannot fully change without JavaScript.
- Never rely on browser validation alone. Check everything again on the server.
HTML Form Validation exercises
Must be filled in
Make a form with an email field named email that has to be filled in, and a submit button Sign up.
Result
