Browsers validate forms natively, blocking submission and showing a message when a constraint is not met. It requires no JavaScript and covers most common requirements, which makes it worth using before reaching for anything more elaborate.
The required attribute
A boolean attribute that prevents submission while the field is empty.
<input type="email" id="email" name="email" required> For a radio group, marking any one option required makes the whole group required. For a checkbox it means that specific box must be ticked, which suits a terms acceptance and does not extend across a group.
Type based validation
Several input types validate their format automatically. The email type checks for something resembling an address, and the url type for something resembling a web address.
The email check is deliberately permissive, because the rules for valid addresses are far more elaborate than most people expect and rejecting unusual but legitimate addresses is worse than accepting a malformed one. It catches obvious mistakes rather than guaranteeing deliverability, and nothing short of sending a message can do the latter.
Range and length constraints
Numeric and date inputs accept min and max attributes, and step controls the permitted increments. Text inputs accept minlength and maxlength.
Note the asymmetry: maxlength prevents typing beyond the limit, whereas minlength only blocks submission. A user can type one character and only discover the requirement when they try to submit, which is an argument for stating the requirement in visible text.
The pattern attribute
For anything the built in types do not cover, pattern takes a regular expression the value must match.
<input type="text" id="postcode" name="postcode" pattern="[A-Za-z]{1,2}[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}" title="Enter a UK postcode, for example SW1A 1AA"> The expression is anchored automatically, so it must match the entire value. The title attribute is used by some browsers in the error message, which is one of the few places a title attribute earns its place, though it should not be the only explanation offered.
Patterns are easy to make too strict. A rule that rejects spaces in a card number or refuses an unusual name format will turn people away, and the cost of a slightly permissive pattern is much lower than the cost of a wrongly rejected user.
Styling validity
CSS pseudo-classes reflect validation state, and one of them needs care.
input:user-invalid { border-color: #b3261e; } input:user-valid { border-color: #1b7f3b; } The plain invalid pseudo-class matches from page load, which means every required field is styled as an error before the user has typed anything. The user-invalid variant only applies after the user has interacted with the field, which is the behaviour almost everyone actually wants.
Turning it off
The novalidate attribute on a form disables browser validation entirely, and formnovalidate on a button disables it for that submission. The latter is the correct way to build a save draft button alongside a full submit.
Disabling validation on the whole form is common when a project implements its own, usually because the native error messages cannot be styled and appear in the browser's language rather than the site's.
The limits of native messages
Native validation shows one message at a time, attached to the first invalid field, and disappears after a moment. It cannot be styled, and its wording is fixed.
For a short form this is adequate. For a longer one, a custom approach that lists every problem at once and leaves the messages on screen is a better experience. The constraint validation interface in JavaScript exposes the same checks, so you can use the browser's own logic while controlling the presentation.
Custom messages
The constraint validation interface lets a script replace the default message with wording of your own, and clear it again once the value becomes acceptable. Forgetting to clear it leaves a field permanently invalid, which is the usual bug when a custom message is introduced.
This is also how a check the attributes cannot express is added, such as confirming two password fields match. The browser has no built-in rule for comparing fields, so the comparison is scripted and the result reported through the same mechanism, which keeps the behaviour consistent with the native constraints around it.
None of this is security
This is the point that matters most. Every constraint here runs in the browser, and the browser is under the control of the person using it. Attributes can be removed with developer tools in seconds, and a request can be sent directly without involving your form at all.
Client side validation exists to help honest users correct mistakes quickly. It stops nobody who is trying. Every value arriving at your server must be validated there, without exception, regardless of what the form appeared to enforce.