HTML Form Validation
HTML Living Standard
HTML form validation uses constraints such as required, min, and maxlength to describe the values a field accepts. The browser usually blocks a normal submission and shows a message if a value breaks a rule. Validate the submitted data on the server too, since a request can skip the browser checks.
On this page
Require a Value with required
Use required when a field must have a value before the form is submitted. The visible label identifies what the user should enter.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Require a Value with required</title>
</head>
<body>
<form>
<label for="guest">Guest name (required)</label>
<input id="guest" name="guest" type="text" required>
<button type="submit">Continue</button>
</form>
</body>
</html>
What to expect Submit with the field empty to see the browser ask for a value. Enter May and submit again.
Set a Range and Step with min, max, and step
min and max set the lowest and highest permitted values. With min="2" and step="2", valid values go up in steps of two from 2. max="10" places the upper limit at 10.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Set a Range and Step with min, max, and step</title>
</head>
<body>
<form>
<label for="tickets">Tickets (2 to 10, in pairs)</label>
<input id="tickets" name="tickets" type="number" min="2" max="10" step="2" required>
<button type="submit">Reserve</button>
</form>
</body>
</html>
What to expect Try 5 for a step mismatch, 12 for a value above the limit, and 6 for an accepted value.
Limit Text Length with minlength and maxlength
minlength gives an entered value a minimum length. maxlength sets the upper limit. A browser usually stops ordinary typing once that limit is reached. Add required if the field also needs to reject an empty value.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Limit Text Length with minlength and maxlength</title>
</head>
<body>
<form>
<label for="reference">Reference (3 to 8 characters)</label>
<input id="reference" name="reference" minlength="3" maxlength="8" required>
<button type="submit">Continue</button>
</form>
</body>
</html>
What to expect Type AB and submit to see a message. Type ABCDEFGHI to see where typing stops. ABC123 passes the length check.
Check a Format with pattern
pattern uses a regular expression to check the entire entered value. [A-Z]{2}[0-9]{4} expects two uppercase English letters followed by four digits. Put the expected format in visible text so the user can see it before submitting.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Check a Format with pattern</title>
</head>
<body>
<form>
<label for="booking">Booking code</label>
<p id="format">Enter two uppercase letters and four digits, such as AB1234.</p>
<input id="booking" name="booking" pattern="[A-Z]{2}[0-9]{4}"
aria-describedby="format" required>
<button type="submit">Look up booking</button>
</form>
</body>
</html>
What to expect Try ab1234 or AB123, then try AB1234. The browser accepts AB1234.
Check a form without submitting it
Call form.reportValidity() when a separate button needs to run the browser’s validation without submitting the form. It displays the browser’s message for an invalid field and returns false. When it returns true, read the valid data using FormData.
How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Check a form without submitting it</title>
</head>
<body>
<form id="order">
<label for="quantity">Quantity (1–5)</label>
<input id="quantity" name="quantity" type="number" min="1" max="5" step="1" placeholder="2" required>
<button type="button" id="check">Check</button>
</form>
<p id="result" aria-live="polite"></p>
<script>
const form = document.querySelector("#order");
const result = document.querySelector("#result");
document.querySelector("#check").addEventListener("click", () => {
result.textContent = "";
if (!form.reportValidity()) return;
const quantity = new FormData(form).get("quantity");
result.textContent = "quantity = " + quantity;
});
</script>
</body>
</html>
What to expect Enter 0 or 6 and press Check to see a validation message. With 2, the result displays quantity = 2.
Test a registration form
Build a registration form with a name, email and course selector. Connect each label using for and id. Give each submitted field a name. Submit empty fields, a malformed email, and then valid values. Observe which field the browser focuses each time.