Forms Lesson 33 of 42
HTML Form Validation — required, min, max, pattern
Use built-in HTML validation — required, minlength, maxlength, min, max, step and pattern — to catch mistakes before a form is submitted.
People make mistakes when filling in forms: they skip a field, type their email without the @, or put "abc" in an age box. Validation means checking the answers before the form is sent. Modern HTML can do a lot of this checking by itself, with just a few attributes and no JavaScript.
required — the field can't be empty
<form>
<label for="nm">Full name (required):</label><br>
<input id="nm" name="name" required>
<button type="submit">Submit</button>
</form>
Press Submit with the box empty. The browser stops the form and shows a message like "Please fill out this field." The message's wording and style depend on the browser and the device language.
Type-based validation
Some input types validate automatically. type="email" checks for the basic shape of an email address, and type="url" checks for a web address.
<form>
<label for="em">Email:</label><br>
<input type="email" id="em" name="email" required value="juan.delacruz">
<button type="submit">Submit</button>
</form>
Submit as is — "juan.delacruz" is rejected because there's no @ and domain. Change it to juan@example.com and it passes.
Length limits: minlength and maxlength
<form>
<label for="un">Username (4–12 characters):</label><br>
<input id="un" name="username" minlength="4" maxlength="12" required>
<button type="submit">Submit</button>
</form>
maxlength simply stops you from typing more. minlength shows an error on submit if the text is too short.
Number limits: min, max and step
<form>
<label for="gl">Grade level (7–12):</label><br>
<input type="number" id="gl" name="grade" min="7" max="12" required>
<button type="submit">Submit</button>
</form>
Type 15 and submit: the browser says the value must be less than or equal to 12. step controls allowed increments — for example step="0.25" for quantities like 1.25 kg, or step="any" to allow any decimal.
min and max work on dates too. For example, an event registration that only allows dates in December 2026:
<input type="date" name="visit" min="2026-12-01" max="2026-12-31">
pattern — your own rule
pattern checks the value against a regular expression, a mini-language for describing text patterns. Regular expressions can get complicated, but a few simple ones are very useful:
<form>
<label for="mob">Mobile number:</label><br>
<input type="tel" id="mob" name="mobile" pattern="09[0-9]{9}" placeholder="09171234567" title="11 digits starting with 09, no spaces" required>
<button type="submit">Submit</button>
</form>
Reading the pattern 09[0-9]{9}:
09— must start with the characters 0 and 9,[0-9]— then any digit from 0 to 9,{9}— exactly nine times.
Total: 11 digits. The title text is added to the error message, so users know what format you expect. Always include it with pattern.
| Pattern | Means | Example match |
|---|---|---|
[0-9]{12} | Exactly 12 digits | An LRN like 123456789012 |
[A-Za-z ]+ | Letters and spaces only | Maria Clara |
[0-9]{4} | Exactly 4 digits | A ZIP code like 1100 |
[A-Z]{3}[0-9]{4} | 3 capital letters + 4 digits | A plate format like ABC1234 |
Styling valid and invalid fields
CSS can react to validation with :valid and :invalid. Using :user-invalid waits until the user has interacted with the field, so the form doesn't look all red before they even start (supported in modern browsers).
<style>
input { padding: 8px; border: 2px solid #ccc; border-radius: 6px; font-size: 16px; }
input:valid { border-color: #1d7a43; }
input:user-invalid { border-color: #b4372b; background: #fbe9e6; }
</style>
<form>
<label for="zip">ZIP code (4 digits):</label><br>
<input id="zip" name="zip" pattern="[0-9]{4}" title="4 digits" required>
<button type="submit">Submit</button>
</form>
Turning validation off: novalidate
Adding novalidate to the <form> turns off the browser's checks. Developers sometimes do this to show their own custom messages using JavaScript. While learning, keep the built-in validation on.
Very important: HTML validation is not security
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04