agyanadda@gmail.com

HTML Form Validation

HTML Forms 1 min read Updated Nov 30, 2026 Topic 25 of 33

HTML can check form fields before the form is sent — no JavaScript needed.

Attribute What it checks
required The field cannot be empty
minlength / maxlength Text length
min / max Number or date range
pattern Must match a pattern (regular expression)
type="email" Must look like an email
placeholder A hint shown inside the empty field

If a field is wrong, the browser shows a message and stops the form.

Important: browser checks are for convenience. Always check data on the server too, because users can bypass browser validation.

Example

Example — Validated sign-up form HTML
<form>
  <input type="text" name="name" placeholder="Full name" required minlength="3"><br><br>
  <input type="email" name="email" placeholder="Email" required><br><br>
  <input type="tel" name="mobile" placeholder="10-digit mobile"
         pattern="[6-9][0-9]{9}" title="Enter a valid 10-digit Indian mobile number" required><br><br>
  <input type="number" name="age" placeholder="Age" min="16" max="60"><br><br>
  <button type="submit">Register</button>
</form>

Try pressing Register with empty or wrong values.

Try it Now »

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which attribute makes a field mandatory?

Exercise 2 True / False

Browser validation is enough; the server does not need to check data.

Exercise 3 Fill in the Blank

To allow only numbers between 1 and 10, use min="1" and ___="10".

Frequently Asked Questions

Add a title attribute to explain the pattern, or use JavaScript setCustomValidity() for a fully custom message.