Skip to content

Beginner · Forms, semantic HTML and accessibility · topic 11 of 64 · 25 min

Built-in form validation

Explanation

HTML can check answers before a form is sent. Attributes like required, minlength, min, max and pattern tell the browser what a good answer looks like, and it shows a message when something is wrong. The server must still check again, because browser checks can be skipped.

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.

Think of it like this: Think of the person at a railway reservation counter who glances at your form and says "you forgot the train number" before it goes into the computer. The browser is that first quick check; the server is the final, careful one.

The validation attributes

AttributeMeaningExample
requiredMust not be empty<input required>
minlength / maxlengthNumber of characters allowed<input minlength="8">
min / max / stepRange for numbers and dates<input type="number" min="18" max="60">
patternA 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.

html
<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">
An Indian mobile number starts with 6, 7, 8 or 9 and has 10 digits

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.

javascript
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());          // true
The browser can tell you if a field is valid
Tip: Error messages should say how to fix the problem: "Enter a 6-digit PIN code", not just "Invalid input".
Careful: Anyone can turn off browser checks (for example with DevTools). Browser validation is for convenience; the server must always validate again.

The 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 = checking answers

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)

Task 1 · Code · HTML

Make the PIN code field in this form required and allow only exactly 6 digits using pattern. Also add inputmode="numeric".

Task 2 · Code · HTML

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.

Task 3 · Practice

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

Question 1

1.Which attribute stops a form from being sent while a field is empty?

Question 2

2.What does pattern="[0-9]{6}" accept?

Question 3

3.Why must the server validate again?

Question 4

4.Which attribute lets the phone fill a saved PIN code?

Question 5

5.Which error message is most helpful?

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.

← Previous: Forms and inputs