Form Validation

Built-in Validation

Browsers can check input before a form is sent, with no JavaScript:

AttributeRule
requiredthe field must not be empty
minlength / maxlengthlength of text
min / max / steprange for numbers and dates
patterna regular expression the value must match
type="email", type="url"format of the value

Example

Example

Press Submit with empty or invalid fields to see the browser messages:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
      body { font-family: Arial, sans-serif; }
      label { display: block; margin-top: 8px; }
      input, select, textarea { padding: 6px; margin-top: 4px; }
      button, input[type=submit] { margin-top: 10px; padding: 6px 14px; }
    </style>
</head>
<body>

<form onsubmit="alert('Form is valid!'); return false;">
  <label>Email (required) <input type="email" name="email" required></label>
  <label>PIN code (6 digits) <input name="pin" pattern="[0-9]{6}" title="Six digits"></label>
  <label>Password (8+ chars) <input type="password" name="pwd" minlength="8" required></label>
  <button>Submit</button>
</form>

</body>
</html>

Result

Client-side Validation Is Not Enough

Note: Users can switch validation off or send requests without your form. Always validate again on the server. Browser validation is for a better user experience, not for security.