Validation
Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.
On this page
How it worksLink to this section: How it works
Here’s how form validation works with OUDS Web:
- HTML form validation is applied via CSS’s two pseudo-classes,
:invalidand:valid. It applies to<input />,<select>, and<textarea>elements. - OUDS Web scopes the
:invalidand:validstyles to parent.was-validatedclass, usually applied to the<form>. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted). - To reset the appearance of the form (for instance, in the case of dynamic form submissions using Ajax), remove the
.was-validatedclass from the<form>again after submission.
- As a fallback,
.is-invalidand.is-validclasses may be used instead of the pseudo-classes. They do not require a.was-validatedparent class. - All modern browsers support the constraint validation API, a series of JavaScript methods for validating form controls.
- Feedback messages may utilize the browser defaults (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS.
- You may provide custom validity messages with
setCustomValidityin JavaScript.