Beginner · Forms, semantic HTML and accessibility · topic 11 of 64 · 25 min
Built-in form validation
Explanation
Why validate?
Validation means checking that an answer makes sense before using it: the mobile number has 10 digits, the age is a number, the email has an @. Checking in the browser gives the user instant help, without waiting for the server.
The validation attributes
| Attribute | Meaning | Example |
|---|---|---|
| required | Must not be empty | <input required> |
| minlength / maxlength | Number of characters allowed | <input minlength="8"> |
| min / max / step | Range for numbers and dates | <input type="number" min="18" max="60"> |
| pattern | A regular expression the text must match | <input pattern="[0-9]{6}"> |
| type="email" / "url" | Must look like an email or URL | <input type="email"> |
A regular expression (regex) is a short pattern that describes text. [0-9] means one digit and {6} means exactly six of them, so [0-9]{6} matches a 6-digit PIN code. The pattern must match the whole value.
<label for="mobile">Mobile number (10 digits)</label>
<input id="mobile" name="mobile" type="tel" required
pattern="[6-9][0-9]{9}" inputmode="numeric" autocomplete="tel">
<label for="pin">PIN code</label>
<input id="pin" name="pin" required pattern="[0-9]{6}"
inputmode="numeric" autocomplete="postal-code">
<label for="age">Age</label>
<input id="age" name="age" type="number" min="18" max="60">Help the phone help the user
- inputmode="numeric" shows a number keyboard even for a text field.
- autocomplete="name", "email", "tel", "postal-code" lets the phone fill saved details in one tap.
- Write the rule next to the label ("10 digits"), not only in the error.
Checking from JavaScript
Every input has a validity object and a checkValidity() method. validity.valueMissing is true when a required field is empty; validity.patternMismatch is true when the text does not match the pattern.
const pin = document.querySelector('#pin');
pin.value = '1100';
console.log(pin.checkValidity()); // false
console.log(pin.validity.patternMismatch); // true
pin.value = '110001';
console.log(pin.checkValidity()); // trueThe novalidate attribute on a form turns the built-in checks off. Developers use it when they write their own checks in JavaScript, which we do later in the course.
Watch the lesson
Built-in form validation · 1/6
Validation means checking answers before using them. The browser can do the first check for free.
Prefer a video by a teacher? Find free videos on “html form validation required pattern tutorial” on YouTube ↗
In real life: A gas cylinder booking form
A booking page asks for a 10-digit mobile number and a 6-digit PIN code. With required, pattern and inputmode="numeric", the phone shows a number keypad, the browser stops the form if a digit is missing, and the customer sees "Enter a 6-digit PIN code" before anything is sent.
Practice (0/3 done)
Make the PIN code field in this form required and allow only exactly 6 digits using pattern. Also add inputmode="numeric".
Build a sign-up form with: an email input that is required, a password input with minlength="8" that is required, and an age input (type number) with min="18". Every input needs a label.
Open your sign-up form in a phone browser (or DevTools device mode). Try to submit it empty, with a wrong email and with a short password. Note the messages you see, then rewrite each label so the rule is clear before the user types.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.