required
The field must have a value before submission.
Learn Form Validation with practical examples for form structure, input behavior, validation, autocomplete, accessibility, backend safety and live practice.
HTML forms
Browsers can validate forms with attributes such as required, minlength, maxlength, min, max, step and pattern.
Native validation is useful because it is fast, accessible when used well and works without writing JavaScript for every basic rule.
Validation should help, not punish. Explain what is expected, keep messages clear and always repeat validation on the server.
The field must have a value before submission.
The text must contain at least a set number of characters.
A regular expression for specific formats. Use carefully.
Disables native validation on a form when custom handling is needed.
Syntax and behavior
Start with simple native constraints. Add JavaScript only when the interaction genuinely needs more.
<label for="code">Access code</label>
<p id="code-help">Use 6 uppercase letters or numbers.</p>
<input
id="code"
name="code"
type="text"
minlength="6"
maxlength="6"
pattern="[A-Z0-9]{6}"
aria-describedby="code-help"
required
>
<input name="code" required pattern="[A-Z0-9]{6}">
<p>Error</p>
Rules that matter
Strong form markup is readable, accessible, secure by design and easy to connect to real server-side validation.
Do not force unnecessary fields.
Pattern rules need visible help text.
Complicated patterns often reject valid real-world input.
Match the rule to the input type.
Users can bypass browser validation.
Tell users exactly what to fix and where.
Production thinking
This matters because validation is part of the conversation with the user. Bad validation makes users feel like the website is fighting them.
Connect instructions and errors with aria-describedby so the message belongs to the right field.
Backend validation is mandatory. Client validation improves UX; server validation protects the system.
Helpful validation reduces form abandonment after SEO traffic reaches a conversion page.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.