Form validation

A form collects input from people, and people leave fields blank, mistype an email, or put letters where a number belongs. You do not want that data reaching your code untouched. Before anything is submitted, the browser can check a lot of it for you, but only if you describe what each field expects. That description is written straight into your HTML, and this chapter is about how to write it.
Required fields and input types
The two cheapest checks you can add are saying that a field must be filled in at all, and saying what kind of value belongs in it.
Add the word required to an input and the browser refuses to submit the form until that field has something in it. You do not write any code to make this happen. The attribute is the instruction.
<input type="text" name="full_name" required>The other half is the type attribute. It tells the browser what shape the value should be, and the browser checks it for you. Use type="email" and the browser makes sure the value looks like an email address. Use type="number" and it only accepts numbers. There is a type for dates, one for web addresses, and more.
Think of it like a form at a doctor's office where some boxes are marked "must fill in" and the date box already has little slots for day, month, and year. The paper is telling you what goes where before you write a thing.
Here is a small sign-up form using both attributes:
<form>
<label>
Email address
<input type="email" name="email" required>
</label>
<label>
Website
<input type="url" name="website">
</label>
<button>Sign up</button>
</form>The email field must be filled in and must look like an email. The website field is optional, but if someone types anything, it has to look like a web address. The browser checks both the moment the button is pressed.
required on a field and the browser will not let an empty one through. Set the type, like email or number, and it checks the value is the right shape. Pick the type that matches the field and you get checking for free. Constraint attributes
Beyond "filled in" and "right type", you often want limits: a value at least this long, a number no larger than that, a code in a set format. A handful of attributes cover this.
You can set limits on what a field accepts. Two useful ones to start with:
<input type="text" name="username" maxlength="20">
<input type="number" name="quantity" min="1" max="10">maxlength="20" stops the field once it holds twenty characters. min and max set the smallest and largest numbers allowed. The browser holds people to these bounds so you do not have to check them yourself.
The rule that saves the most confusion: a pattern must match the whole value, not only part of it.
<input
type="text"
name="product_code"
pattern="[A-Z]{2}-[0-9]{3}"
title="Two letters, a hyphen, three digits, e.g. AB-123"
required
>maxlength caps how many characters go in, and min and max fence in a number. The browser keeps people inside those bounds automatically, which is a lot of checking you never have to write. Native validation feedback
Declaring the rules is half the story. The other half is what the person sees when a value breaks one.
When someone presses the submit button and a field is wrong, the browser stops the submission and shows a small message next to the first field with a problem, then jumps the cursor to it. You do not build this message. The browser writes it, in the visitor's own language, and shows it automatically.
So a required email field left blank produces something like "Please fill in this field", and the form does not send until it is fixed.
When you still need JavaScript
Native validation checks one field against its own rules. Plenty of real checks do not fit that shape, and those are where JavaScript comes in.
Some things the browser cannot check on its own. Whether two password fields match each other. Whether a username is already taken by someone else. Whether a discount code is real. None of these are about one field's shape, so there is no attribute for them, and you reach for JavaScript to do the checking.
That is not a failing of HTML. The built-in checks handle the common cases with no code, and JavaScript handles the rest.

