Loading content…
Loading content…
Master native HTML5 form validation and design accessible input structures using modern ARIA guidelines
required: Field must be filled before submission.pattern: Regex schema matching constraint.min/max: Minimum and maximum numerical bounds or dates.minlength/maxlength: Text length bounds.type: Standard browser input type checking (email, url, number, tel).<form id="profileForm">
<!-- Validating an input with custom pattern, length limits, and requirements -->
<label for="username">Username (Letters and numbers only):</label>
<input
type="text"
id="username"
name="username"
required
minlength="4"
maxlength="15"
pattern="[a-zA-Z0-9]+"
title="Username must be 4-15 characters and contain only letters and numbers."
>
<button type="submit">Save Profile</button>
</form>
.validity object:const emailInput = document.getElementById("email");
emailInput.addEventListener("input", () => {
if (emailInput.validity.typeMismatch) {
emailInput.setCustomValidity("Please enter a valid email address!");
} else if (emailInput.validity.valueMissing) {
emailInput.setCustomValidity("Email is required!");
} else {
emailInput.setCustomValidity(""); // Clear the error state
}
});
<label>.<!-- ✅ GOOD: Explicit linkage using 'for' matching input 'id' -->
<label for="userEmail">Email Address:</label>
<input type="email" id="userEmail" name="email">
<!-- ❌ BAD: Implicit or missing links (hard for screen readers to associate) -->
<span>Email Address:</span>
<input type="email" name="email">
outline: none) without providing a visible, high-contrast alternative. Keyboard-only users rely on the focus ring to see where they are on the page./* Custom focus rings are recommended for custom styling */
input:focus, select:focus, textarea:focus {
outline: 3px solid #6366f1; /* High contrast Indigo */
outline-offset: 2px;
}
aria-describedby to associate helper text or validation error logs with their inputs:<label for="passCode">Pin Code:</label>
<input
type="password"
id="passCode"
name="passcode"
aria-describedby="passcodeHelper passcodeError"
required
>
<small id="passcodeHelper">Your pin must be 4 digits.</small>
<span id="passcodeError" class="error-msg" aria-live="assertive">
Incorrect code format.
</span>
aria-live automatically announces dynamic text changes (e.g. error alerts) to screen reader users.aria-live="polite": Announces when the user is idle.aria-live="assertive": Interrupts the user immediately (use for critical error notifications).Senior Developer Wisdom
Common Pitfall
display: none or visibility: hidden removes them from the screen reader accessibility tree entirely. If you want to visually hide labels (e.g., in icon-only buttons) while keeping them accessible to screen readers, use a visually-hidden CSS utility class./* visually-hidden helper */
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
}
<button type="submit">
<svg aria-hidden="true" class="icon"><!-- SVG icon --></svg>
<span class="sr-only">Submit Search Query</span>
</button>
Form Validation & Accessibility Checklist
<label>, <input>) are preferred over custom styled divs.Marking it complete updates your roadmap progress percentage.