Performing Custom Validation

If you need to add custom validation rules to a form element then you can take advantage of the JavaScript setCustomValidity() method. You can use this method to associate a custom validation error message with a form field.

Imagine, for example, that you have a complex set of rules for validating a username in a user registration form. For example, you want to ensure that the username is a certain length, unique in the database, and does not contain special characters. This is a good candidate for a custom validation.

The following HTML form includes a userName field:

<form>    <div>        <label>            User Name:            <input id="userName" required />        </label>     ...

Get Windows® 8.1 Apps with HTML5 and JavaScript Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.