Before reaching for a validation library, use the browser. Native HTML forms already block bad submissions, provide keyboard hints, and expose a JavaScript API for the cases you cannot style or explain with markup alone. The trick is knowing where native validation ends and accessibility work begins.

1) Start with the correct input type
The most reliable validation is often the input type itself. Use type="email" for email, type="tel" for telephone numbers, type="url" for URLs, type="number" for numeric fields with spinners, and type="date" or type="datetime-local" for dates.
Do not assume every numeric-looking field should be type="number". Credit cards, ZIP codes, two-factor codes, and account numbers can contain leading zeros, spaces, or grouping characters. A text input with inputmode and a pattern is often better.

2) Add constraint attributes
required
required prevents submission when the control is empty. It works on inputs, selects, and textareas that participate in constraint validation.
minlength and maxlength
minlength="8" rejects values shorter than eight characters after submission. maxlength="40" usually prevents further typing, which can confuse users who paste a truncated name. Keep the rule visible, such as “8–40 characters,” and do not treat maxlength as a substitute for an error message.
min, max, and step
For dates, times, and numeric inputs, use min and max. step defines allowed increments from the base value. For example, step="0.01" means values must fit hundredths, so 19.99 is valid but 19.999 is not. It constrains the value; it does not merely format two decimal places.
pattern
pattern uses a JavaScript regular expression, but you do not need JavaScript to run it. It is anchored around the whole value for most text-like inputs, so write pattern="[A-Za-z0-9_]{3,16}", not one that only finds a match somewhere.
Patterns are especially useful for usernames, coupon codes, and reference numbers. Keep them understandable. A brutal password regex often creates worse UX than separate rules such as length and forbidden common passwords.
3) Use inputmode and autocomplete deliberately
inputmode gives mobile browsers a keyboard hint. It does not validate. Use inputmode="numeric" with a numeric keypad, inputmode="email" for email entry, or inputmode="tel" for phone keyboards. Do not write the broken phrase “inputmode="numeric" with inputmode or autocomplete”; choose the attribute that matches the data and combine it with validation separately.
autocomplete helps password managers and browsers fill known fields. Use values such as email, current-password, new-password, postal-code, cc-number, and billing address-line1. Correct autofill reduces typing errors more than another regex.
4) Style validation states with CSS
The :invalid pseudo-class is tempting, but it marks fields invalid immediately, often before a user has typed anything. Prefer :user-invalid, which activates after interaction or a blocked submit:
input:user-invalid { border-color: #c0392b; }
Browser support for :user-invalid is good in modern evergreen browsers but not universal. Add a fallback class with JavaScript if older support matters:
input:not(:placeholder-shown):invalid can help for empty placeholders, but a .touched:invalid class is more predictable. Also style :valid carefully; green borders on every empty optional field are noisy.
5) When to use novalidate and the Constraint Validation API
Without novalidate: keep native blocking
If you omit novalidate, the browser blocks submit and shows its built-in bubble:
<form><input required><button>Save</button></form>
This is the smallest accessible path, but styling and message wording are browser-dependent.
With novalidate: own the submit flow
Add novalidate only when you will provide custom UI. Then call reportValidity() or inspect fields yourself.
<form novalidate id="signup">
<label>Email
<input name="email" type="email" required
aria-describedby="email-error">
</label>
<p id="email-error" class="error" aria-live="polite"></p>
<button>Create account</button>
</form>
<script>
const form = document.querySelector('#signup');
form.addEventListener('submit', (event) => {
const fields = form.querySelectorAll('input');
let firstInvalid = null;
fields.forEach((field) => {
const error = field.nextElementSibling;
field.setCustomValidity('');
if (!field.checkValidity()) {
field.setAttribute('aria-invalid', 'true');
error.textContent = validationMessageFor(field);
firstInvalid ||= field;
} else {
field.removeAttribute('aria-invalid');
error.textContent = '';
}
});
if (!form.checkValidity()) {
event.preventDefault();
firstInvalid?.focus();
}
});
</script>
Use setCustomValidity() for rules the native attributes cannot express, such as checking that two email addresses match. Remember to clear it on input; otherwise a stale custom error keeps the field invalid forever. The title attribute can describe a pattern, but it is not enough as the only visible instruction and should not be used as a replacement for label or help text.
| Need | Native HTML/CSS | Use JS |
|---|---|---|
| Required field | required | Rarely |
| Email/URL/date type | type | Only for stricter domain rules |
| Length and numeric range | minlength, maxlength, min, max | Custom counters or cross-field rules |
| Allowed format | pattern, step | Complex checksum or remote lookup |
| Mobile keyboard | inputmode | No |
| Autofill | autocomplete | No |
| Inline accessible errors | Limited native bubbles | aria-invalid, aria-describedby, focus management |
| Server-side uniqueness | Impossible alone | Fetch API plus server validation |
6) Accessibility and server validation are not optional
Every field needs a visible <label>. Associate error text with aria-describedby, mark bad fields with aria-invalid="true", and place a summary or field message in an aria-live="polite" region when errors update after submit. Move focus to the first invalid field, or to an error summary that links to each field.
Do not rely on color alone. Add text, an icon, border weight, or another non-color cue. Most importantly, revalidate on the server. Client-side validation improves UX; it is not a security boundary. A user can submit arbitrary HTTP data without your form.
7) Copyable registration example
<form novalidate class="card">
<div class="field">
<label for="email">Email</label>
<input id="email" name="email" type="email" required
autocomplete="email" inputmode="email"
aria-describedby="email-error">
<p id="email-error" class="error" aria-live="polite"></p>
</div>
<div class="field">
<label for="phone">Phone</label>
<input id="phone" name="phone" type="tel" required
pattern="[0-9 ()+\-]{7,20}" inputmode="tel"
autocomplete="tel" aria-describedby="phone-error">
<p id="phone-error" class="error" aria-live="polite"></p>
</div>
<div class="field">
<label for="password">Password</label>
<input id="password" name="password" type="password"
required minlength="8" maxlength="128"
autocomplete="new-password"
aria-describedby="password-help password-error">
<p id="password-help">Use 8–128 characters.</p>
<p id="password-error" class="error" aria-live="polite"</p>
</div>
<div class="field">
<label for="donation">Donation</label>
<input id="donation" name="donation" type="number"
min="1" max="1000" step="0.01" value="10.00">
</div>
<button type="submit">Create account</button>
</form>
Native constraints remove a lot of boilerplate. Use them first, enhance only when the UI must be custom, and keep your server as the final validator.