Web Analytics

HTML Form Validation

Intermediate ~10 min read

HTML5 Form Validation

HTML5 provides built-in form validation without JavaScript. The browser automatically validates input before submission.

The Required Attribute

The required attribute makes a field mandatory. The form won't submit until it's filled.

HTML
CSS
JS

The Pattern Attribute

The pattern attribute specifies a regular expression that the input must match.

HTML
CSS
JS
Tip: Use the title attribute to explain the expected format to users.

Min, Max, and Length

  • min / max - For numbers and dates
  • minlength / maxlength - For text length
  • step - Specifies valid number intervals
HTML
CSS
JS

Other Validation Attributes

  • type="email" - Validates email format
  • type="url" - Validates URL format
  • type="number" - Only accepts numbers
  • readonly - Field cannot be modified
  • disabled - Field is disabled and not submitted

Quick Quiz

Which attribute uses regular expressions for validation?

A
required
B
pattern
C
validate
D
regex