Form Validation
People make mistakes when filling in forms: they skip fields, mistype email addresses or enter "abc" where a number belongs. HTML has built-in validation that checks input in the browser before the form is sent, using simple attributes such as required, minlength, min, max and pattern. It needs no JavaScript.
How built-in validation works
When the user submits a form, the browser checks every control against its rules. If any control is invalid, the browser:
- cancels the submission,
- moves focus to the first invalid control,
- shows a short message next to it, such as "Please fill out this field." (the exact wording and style vary between browsers and languages).
Nothing is sent until every rule passes.
The validation attributes
required
A boolean attribute that makes a field mandatory. It works on text-like inputs, checkboxes, radio groups, <select> and <textarea>.
<input type="text" id="name" name="name" required> <input type="checkbox" id="terms" name="terms" required>
A required checkbox must be ticked. For a radio group, putting required on any one radio in the group means one option must be chosen.
Type checks
Some types validate automatically. type="email" rejects values without a sensible name@domain shape, and type="url" rejects values that aren't full URLs. Number and date inputs only accept valid numbers and dates.
Length: minlength and maxlength
<input type="password" id="pw" name="password" minlength="8" required> <textarea id="bio" name="bio" maxlength="200"></textarea>
minlength rejects shorter values on submit. maxlength works differently: the browser simply stops the user typing more characters.
Ranges: min, max and step
<input type="number" id="age" name="age" min="17" max="26" required> <input type="number" id="amount" name="amount" min="50" step="50"> <input type="date" id="start" name="start" min="2027-01-04">
The age must be from 17 to 26. The amount must be at least 50 and a multiple of 50 counting from the minimum, so 50, 100 and 150 pass but 120 fails. The date can't be before 4 January 2027.
pattern
pattern checks text against a regular expression, a mini-language for describing text shapes. The pattern must match the whole value, not just part of it.
<label for="phone">Safaricom or Airtel number (10 digits)</label>
<input type="tel" id="phone" name="phone" pattern="0[17][0-9]{8}"
title="10 digits starting with 07 or 01, e.g. 0712345678" required>
This reads: a 0, then a 1 or 7, then exactly eight more digits. "0712345678" passes; "712345678" (only nine digits) and "0812345678" (wrong second digit) fail. Many browsers add the title text to their error message, so use it to explain the expected format.
| Pattern piece | Means |
|---|---|
[0-9] | Any one digit |
[A-Z] | Any one capital letter |
[17] | Either 1 or 7 |
{8} | Exactly 8 of the previous item |
{2,4} | Between 2 and 4 of the previous item |
For example, pattern="K[A-Z]{2} [0-9]{3}[A-Z]" matches Kenyan number plates written like "KDA 123A".
Styling valid and invalid fields
CSS can react to validation state with pseudo-classes. :invalid and :valid apply immediately, so a required field is already "invalid" before the user has typed anything, which can look alarming. The newer :user-invalid only applies after the user has interacted with the field, and is supported in current versions of the major browsers.
input:user-invalid { border: 2px solid crimson; }
input:valid { border: 2px solid seagreen; }
Turning validation off
Add novalidate to the <form> to switch off the browser's checks, usually because you're writing your own JavaScript validation with custom messages. Add formnovalidate to one specific submit button, such as "Save draft", to skip validation only for that button.
Browser validation is for convenience, not security. Anyone can open developer tools, delete the required attribute and submit anything, or send data to your server without using your form at all. Always validate again on the server (for example in your Django view) before saving or acting on data.
Try it
Press Sign up with empty fields, then try invalid values. The script at the bottom only runs when every field passes, because the browser blocks submission until then.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Form validation</title>
<style>
input:user-invalid { outline: 2px solid crimson; }
label { display: block; margin-top: 10px; }
</style>
</head>
<body>
<h1>Hackathon sign-up</h1>
<form>
<label for="name">Full name (required)</label>
<input type="text" id="name" name="name" required minlength="3">
<label for="email">Email (required)</label>
<input type="email" id="email" name="email" required>
<label for="age">Age (17 to 26)</label>
<input type="number" id="age" name="age" min="17" max="26" required>
<label for="phone">Phone (10 digits, starts 07 or 01)</label>
<input type="tel" id="phone" name="phone" pattern="0[17][0-9]{8}"
title="10 digits starting with 07 or 01" required>
<label><input type="checkbox" name="rules" required> I accept the rules</label>
<p><button type="submit">Sign up</button></p>
</form>
<p id="result"></p>
<script>
document.querySelector("form").addEventListener("submit", function (event) {
event.preventDefault();
document.querySelector("#result").textContent = "All fields are valid. The form would be sent now.";
});
</script>
</body>
</html>
With empty fields, the browser jumps to "Full name" and shows a message. Try "Jo" (too short), an email without @, an age of 30, or the phone "12345". Each time the browser stops at the first problem. Fields you have edited and left invalid get a red outline. When everything is correct, the message "All fields are valid. The form would be sent now." appears below the form.
Key points
- Browsers check form rules on submit, stop at the first invalid field and show a message.
required,minlength/maxlength,min/max/step,patternand input types define the rules.patternmust match the whole value; explain the format in the label ortitle.- Client-side validation helps users, but the server must always validate again.
Tip: use the ← and → keys to move between pages.
