Advanced form handling and validation with the Constraint Validation API

Anton Ioffe - November 6th 2023 - 7 minutes read

Dive into the intriguing world of JavaScript form handling and validation as we explore the robust capabilities of the HTML5 Constraint Validation API. This comprehensive guide will illuminate the evolution and significance of this API, its fundamental elements, methods, and practical applications, all the way to its limitations. You'll explore the depths of the ValidityState object and the setCustomValidity() method, with a sprinkling of case studies and ample code examples to solidify your comprehension. Ready to enhance your modern web development arsenal? Join us as we unlock the power and potential of the Constraint Validation API.

Background and Overview of HTML5 Constraint Validation API

In the realm of web development, dealing with forms and their validation has always been a task of paramount importance but riddled with complexities. With the emergence of HTML5 and modern DOM APIs, we've moved a long way from manually writing extensive JavaScript code for validation. Introduced in 2008, HTML5 brought forth the Constraint Validation API that significantly simplifies form validation work, allowing even complex constraints to be managed with ease.

The Constraint Validation API operates in conjunction with enhanced HTML5 form validation attributes, providing a more efficient way to manage validation rules. Several essential attributes such as type, pattern, required, minlength, maxlength, min, and max play fundamental roles in controlling validation. For example, by merely adjusting the type attribute for an input element, you can perform basic validations like ensuring a valid email address or a URL.

Given the functionality of these attributes, the required attribute ensures that the field it is associated with cannot be left blank while min and max control the permissible range for numeric input fields. Similarly, minlength and maxlength govern the allowed length for text inputs. For more complex validation, the pattern attribute comes into play, allowing for the specification of a regular expression that the input must correspond to.

While these attributes perform the task of setting up the validation rules, the HTML5 Constraint Validation API serves as the backbone of form validation. This API integrates seamlessly with the form validation attributes and allows for custom validation logic to be formulated. Thanks to the API, the validation process is more streamlined and developer-friendly, eliminating the complications of client-side validation. The symbiotic relationship between the HTML5 Constraint Validation API and form validation attributes offers developers a powerful toolset to create efficient and user-friendly web forms.

Overview of Key Components within the Constraint Validation API

The DOM's Constraint Validation API pairs perfectly with HTML5 validation attributes to give us a formidable control over form handling. We have objects and methods at our disposal that ease the task of form validation, thus reducing reliance on external libraries and boilerplate code.

One key method offered by the API is checkValidity(). When tested on an input field, this method assesses the validity rules set through HTML5 validation attributes and returns a boolean value, expressing whether or not the input adheres to the defined constraints.

let inputField = document.querySelector('input[name="username"]');

In the given case, checkValidity() is employed to examine the input field. A return value of true indicates that the value of the username field has successfully met all the requirements defined in the validation rules. Should it fall short of doing so, the method will return false.

Strengthening the validation process is another admirable utility - the willValidate property. This property returns true if an element will be subjected to validation, and false if not.

let selectField = document.querySelector('select[name="country"]');

Here, willValidate is used to check if our select element falls under the validation umbrella. If it does, true is returned, and if it's exempt from validation, false is returned.

Furthermore, the API also accommodates for custom messages that communicate validation feedback to users. This is realized through the validationMessage property. It provides a string detail of custom validation messages, or a default message if no custom message has been set.

let emailField = document.querySelector('input[name="email"]');

In this example, validationMessage property is used to fetch the validation message set against our email input field. If a custom message is not specified, it will fetch the default message provided by the browser.

Through capably returning the status of validation processes, or precision in detailing the reason for validation failure when encountered, the Constraint Validation API provides an innovative ensemble of methods and tools, effectively reshaping the landscape of form validation. It does so by putting into the hands of the developer a more strategic and granular control over form-validation management.

Practical Applications including Common Scenarios and Case Studies of the Constraint Validation API

Time to dive into the nuts and bolts of the practical application of the Constraint Validation API through common scenarios and case studies. A simple yet effective scenario could involve email and URL validation. HTML5 provides us with the type attribute that simplifies the task. Here is a basic example:

<input type='email' /> // The field value must be an email
<input type='URL' />   // The field value must be a URL

This elegantly applies default validation to the form fields, but as the demands of our forms grow in complexity, the setCustomValidity() method offered by the Constraint Validation API steps in and helps us handle situations where a more advanced validation is required. Consider a scenario where two form fields are used for password and password confirmation. Ensuring that these values match can be achieved using custom validation logic and we can propagate these results back into the form validity state.

let password = document.getElementById('password');
let confirmPassword = document.getElementById('confirmPassword');

confirmPassword.addEventListener('input', function() {
    if(confirmPassword.value !== password.value) {
        confirmPassword.setCustomValidity('Passwords do not match');
    } else {

The above code listens for input events on the confirmPassword field and checks if it matches the password field. If they don't match, it sets a custom validation message; otherwise, it clears any existing custom validation message. The use of the Constraint Validation API in this scenario verifies the password inputs match without using a heavy external library.

While the advantages of performance, memory efficiency, and simplicity become evident in such scenarios, one disadvantage is the responsibility it bestows on developers. The flexibility of handling custom validation logic means developers need to be cautious about consistently following best practices, as the API will not do this automatically.

In-Depth Exploration of ValidityState and setCustomValidity

Diving deep into the ValidityState object, we find that it reflects the validation state of an input field at a given point in time. This object is accessed through the validity property in the input tags within a form. The most intriguing aspect of this object is that it comprises several boolean flags, among which, the most important one is the validflag. This flag returns true if and only if all the validation checks pass. But, as soon as an input becomes invalid due to a recent change in its value, the valid flag promptly turns false. Consider this code snippet:

let myInput = document.querySelector('input');
console.log(myInput.validity.valid); // checks the validation status of the input field 

In the realm of form validation, setCustomValidity() is an indispensable method. This method allows you to flag an input field as invalid and also set a custom error message that will be displayed if the input field doesn't pass the validation checks. Remember that setCustomValidity() only sets the validation state to invalid, it doesn't inherently perform any checks. You are to provide your own logic for the validation process. Check out this example:

function validatePasswords(passwordField, confirmPasswordField) {
    if (passwordField.value !== confirmPasswordField.value) {
        confirmPasswordField.setCustomValidity('Password does not match');

When working with forms, often situations arrive where instantaneous feedback is beneficial. During user signups, it's handy to let the user know instantly if a username is already taken. You can perform this asynchronous validation and then set the result with setCustomValidity(). The prospect of asynchronous validation with setCustomValidity opens up a multitude of creative possibilities.

In the world of complex web applications, forms are not relegated to simple user input fields. Custom or advanced validation rules are part and parcel of this complexity. The Constraint Validation API admittedly cannot presume all disparities that these validation rules can encompass. Despite the fact that setCustomValidity() and the flags provided by ValidityState make it much easier to navigate, you as the developer are expected to sculpt your unique validation requirements.

function validateAge(ageField) {
    if (ageField.value < 21) {
        ageField.setCustomValidity('You must be at least 21 to register');

Summing it up, the ValidityState object is a powerful tool that provides a considerable degree of control over the validation process. It lets you examine the validation state in real time, combine standard HTML5 validations with custom validation logic, and quickly adapt the form control's validation UI for improved user experience.

Limitations of the Constraint Validation API

Although the Constraint Validation API provides a vast array of tools for form validation, it is not without its shortcomings. Its primary limitation lies in the fact that it requires explicit intervention for some operations. A typical case is when the novalidate attribute is set, often used to disable in-browser messages. With this attribute in place, the validation flow needs to be manually orchestrated.

In a scenario where validation should only occur upon form submission, crafting the code for setting this behavior up isn't vastly complex. However, complications arise when validation is required to occur sooner, such as when an input field loses focus. Handling such a case would involve listening for the blur event and, within the event handler, running any custom validation before calling checkValidity() on the input field.

Creating custom validation logic is another area where the API might fall short. While it can certainly facilitate setting up such logic, creating a complex validation structure may prove demanding and strenuous. Furthermore, it might struggle to integrate with applications on a large scale or those with a more advanced validation scenario.

Displaying error messages is another aspect where manual intervention is mandatory. As with other cases, the issue is not the magnitude of the additional code required, but the explicitness of the necessity. This makes it important to carefully consider the project requirements before deciding to go all in with the Constraint Validation API, as it may not be entirely sufficient for more complex applications.


In this article, we explored the HTML5 Constraint Validation API and its powerful capabilities for form handling and validation in modern web development. We discussed the background and overview of the API, delved into its key components, and provided practical applications and case studies. The API allows for easier form validation through enhanced validation attributes and custom validation logic. However, it does have limitations, such as requiring explicit intervention for certain operations and potentially struggling with complex validation scenarios. As a challenging task, readers are encouraged to create a form validation system that combines HTML5 validation attributes and custom validation logic, ensuring a seamless user experience.

Don't Get Left Behind:
The Top 5 Career-Ending Mistakes Software Developers Make
FREE Cheat Sheet for Software Developers