HTML Forms
Form Validation
HTML can check user input before the form is sent, with no JavaScript at all. The browser shows a message and stops the submission if something is wrong.
| Attribute | Rule |
|---|---|
required | Must not be empty. |
type="email" / "url" | Must look like an email / web address. |
minlength / maxlength | Length of text. |
min / max / step | Range of numbers and dates. |
pattern | Must match a regular expression. |
<form>
<input type="text" name="username" required minlength="3" maxlength="15">
<input type="email" name="email" required>
<input type="tel" name="phone" pattern="03[0-9]{9}" title="Format: 03001234567">
<input type="number" name="age" min="13" max="99">
<button>Sign up</button>
</form>