HTML form validation: required, pattern, min, max and more
Make browsers check answers before sending: required fields, length and number limits, patterns and friendly feedback.
In this lesson you will
- Make fields required
- Limit text, numbers and dates
- Write simple patterns with helpful titles
- Style valid and invalid fields
About 13 minutes
Catch mistakes before they travel
A customer fills in your order form, forgets the phone number, mistypes the email and presses send. You receive an order you cannot confirm. Each form like that costs you a sale and costs the customer a frustrating wait. The cure is validation: checking answers before the form is sent. HTML can do a surprising amount of it with no JavaScript at all.
Required fields
Add the required attribute and the browser refuses to send the form until the box is filled in, showing a message beside the empty field.
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
Because the type is email, the browser also checks the shape. Typing priya without an @ stops the form with a polite message. You wrote no code for that.
Press the Send button with the box empty and see what happens, then make the name box required and try again.
<form>
<label for="n">Name</label>
<input type="text" id="n" name="name">
<button type="submit">Send</button>
</form>
Add the word required inside the input tag, after name=”name”.
<form>
<label for="n">Name</label>
<input type="text" id="n" name="name" required>
<button type="submit">Send</button>
</form>
Length and range limits
minlengthandmaxlengthset the fewest and most characters in text boxes.minandmaxlimit numbers and dates.stepcontrols the jump of a number, for examplestep="5"allows 0, 5, 10 and so on.
<input type="text" name="username" minlength="4" maxlength="16" required>
<input type="number" name="age" min="18" max="99" required>
Patterns for custom rules
What if the rule is special, such as a product code made of three capital letters followed by four digits? The pattern attribute accepts a regular expression, a compact description of allowed text.
<label for="code">Product code</label>
<input type="text" id="code" name="code"
pattern="[A-Z]{3}[0-9]{4}"
title="Three capital letters then four digits, for example ABC1234"
required>
Read the pattern from left to right: [A-Z]{3} means three capital letters, and [0-9]{4} means four digits. The title text is shown in the browser’s error message, so write it as a plain-language instruction.
A pattern that rejects input with no explanation feels like a trap. Put the rule in the title, or in a short note under the field, so people know how to fix it.
Styling valid and invalid fields
CSS can react to validation with the pseudo-classes :valid and :invalid. A very common trick is to show a coloured border only after someone has typed something wrong.
input:user-invalid { border: 2px solid #D63384; }
input:user-valid { border: 2px solid #1E9E6A; }
Use :user-invalid and :user-valid rather than the older :invalid. The old version flags an empty required field in red the moment the page loads, before anyone has touched it, which feels unfair.
Buttons and the novalidate switch
A form can have more than one button. A normal type="submit" button validates. A type="button" does nothing on its own and is meant for JavaScript. If you ever need to turn browser checking off, for example while building a form, add novalidate to the form tag, but remember to remove it later.
You can also give a submit button its own destination with formaction, or skip validation for it with formnovalidate. This is handy for a “Save as draft” button that should accept an incomplete form.
<button type="submit">Send order</button>
<button type="submit" formnovalidate formaction="/draft">Save draft</button>
Validation is a courtesy, not security
Browser validation can be bypassed in seconds by anyone who opens the developer tools or sends a request directly. Never rely on it for safety. It exists to help honest visitors, while the server must always check the data again. Treat the HTML rules as a friendly first line and the server rules as the real lock.
A complete signup form
<form action="/signup" method="post">
<label for="user">Username</label>
<input type="text" id="user" name="user" minlength="4" maxlength="16" required>
<label for="mail">Email</label>
<input type="email" id="mail" name="mail" required>
<label for="age">Age</label>
<input type="number" id="age" name="age" min="13" max="99" required>
<label><input type="checkbox" name="terms" required> I accept the terms</label>
<button type="submit">Create account</button>
</form>
Make the form strict: the code must match three digits (pattern), the email must be required, and the year must be a number from 1990 to 2026.
<form>
<input type="text" name="code" placeholder="Code">
<input type="email" name="mail" placeholder="Email">
<input type="number" name="year" placeholder="Year">
<button type="submit">Check</button>
</form>
Use pattern=”[0-9]{3}” for the code, required on all, and min=”1990″ max=”2026″ on the year.
<form>
<input type="text" name="code" placeholder="Code" pattern="[0-9]{3}" title="Three digits" required>
<input type="email" name="mail" placeholder="Email" required>
<input type="number" name="year" placeholder="Year" min="1990" max="2026" required>
<button type="submit">Check</button>
</form>
Writing error messages people can use
The browser’s built-in messages are functional, but you can improve them. A message such as “Please match the requested format” tells people they are wrong without telling them how to be right. The cure is the title attribute for patterns, and nearby helper text for everything else. Compare these two versions of the same field.
<!-- Cold -->
<input type="text" name="pin" pattern="[0-9]{4}" required>
<!-- Helpful -->
<label for="pin">PIN code</label>
<input type="text" id="pin" name="pin" pattern="[0-9]{4}" inputmode="numeric"
aria-describedby="pin-help" required>
<small id="pin-help">Exactly four digits, for example 4821.</small>
The aria-describedby attribute links the hint to the field, so screen readers read it right after the label. The inputmode="numeric" shows the number keypad on phones without limiting the field to number type behaviour. Together they turn a frustrating field into a friendly one.
A short validation checklist
- Does every field that needs an answer have
required? - Does every special rule appear in words next to the field, not only in a pattern?
- Do you keep the typed answers if validation fails? Browsers do this for you, which is another reason to use native validation.
- Is the server checking everything again?
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Which attribute stops a form from sending when a box is empty?
neededrequiredmustfilled
required makes the browser block submission until the field has a value.
What does pattern="[0-9]{4}" allow?
- Any four characters
- Exactly four digits
- Four letters
- Up to four numbers
[0-9] means a digit, and {4} means exactly four of them.
Why should you add a title to a pattern input?
- It speeds up the page
- It explains the rule in the error message
- It makes the field required
- It hides the box
The title text appears in the error so people know how to fix their answer.
Is browser validation enough to keep a site safe?
- Yes, always
- No, the server must check again
- Only on mobile
- Only for numbers
Visitors can bypass browser checks, so the server must validate.
Which attribute lets a button submit without validating?
formnovalidateskipnocheckdraft
formnovalidate on a submit button skips the browser’s checks.
Summary
- Required, min, max, minlength, maxlength and pattern validate without JavaScript.
- Explain every rule to the visitor.
- Use :user-invalid and :user-valid for feedback.
- The server must always validate again.
Questions
Do I need JavaScript for validation?
Not for the basics. HTML attributes cover required fields, ranges, lengths and patterns. JavaScript is used for custom messages and complex rules.
How do I write a regular expression?
Start with the simple ones from this lesson. Character sets in square brackets and counts in curly braces solve most cases. You can learn more later in the JavaScript course.
Why does my pattern not work?
Patterns must match the whole value, and they only run on non-empty fields. Check for a missing required attribute.
