DevAcademy
LearnHTMLForm Validation
IntermediateHTML

Form Validation

Learn how to validate form input natively in HTML using required, patterns, and other validation attributes.

Reading Time

16 min

Lesson

Lesson 16 of 27

Native HTML Validation

Modern browsers can validate form input automatically, before any JavaScript runs, using built-in attributes. This is called constraint validation.

Validation Attributes

AttributeEffect
requiredField must be filled before the form can submit
minlength / maxlengthRestricts the number of characters
min / maxRestricts numeric or date range
patternRequires the value to match a regular expression
stepRestricts numeric input to multiples of a value

Required and Length Validation

<input type="text" name="username" required minlength="3" maxlength="20" />
Output

Pattern Validation

<input
  type="text"
  name="zip"
  pattern="[0-9]{5}"
  title="Enter a 5-digit ZIP code"
  required
/>
Output

Numeric Ranges

The min, max, and step attributes work with number, range, and date inputs to constrain acceptable values.

Numeric Range Validation

<input type="number" name="age" min="18" max="99" step="1" required />
Output

Built-in Type Validation

Types like email and url validate their format automatically. An input of type="email" will reject a value without an @ symbol, with no extra attributes needed.

Email Validation

<input type="email" name="email" required />
Output

Custom Error Messages

The title attribute is shown as part of the validation message when a pattern doesn’t match, giving users a hint about the expected format.

HTML Validation is Not Enough Alone

Native HTML validation improves user experience but can be bypassed (e.g. by disabling JavaScript-free form submission via dev tools). Always validate data again on the server.

Best Practice

Use HTML validation attributes as the first line of defense for a fast, accessible experience, and always re-validate submitted data on the server before trusting it.

Interview Questions

Quick Quiz

1. Which attribute makes a field mandatory before submission?

2. Which attribute restricts input to match a regular expression?

3. Why should you also validate data on the server even if HTML validation passes?