Use by adding data attributes to the field for the type of error your messaging is for.
// User jQuery to listen to the fields changing and attach custom messages jQuery('input, select, textarea') .each(function(){ validate(this); }) .bind('input', function(){ validate(this); }); jQuery('select') .each(function(){ validate(this); }) .bind('change', function(){ validate(this); }); function validate(input) { // Check to see if browser supports custom validity if(! input.validity) return; // If it does make a list of possible error states var validation_types = ['valueMissing', 'typeMismatch', 'patternMismatch', 'tooLong', 'rangeUnderflow', 'rangeOverflow', 'stepMismatch']; // Check each error state for(var i = 0; i < validation_types.length; i++) { // See if a custom message is set var msg = jQuery(input).attr('data-'+validation_types[i]); // See if the field has this type of error if(input.validity[validation_types[i]] && msg) { // override the default browser message input.setCustomValidity(msg); // exit out of the loop break; }else { input.setCustomValidity(null) } // Otherwise ignore } }
Create an email signup form that has a required email input and browser validation.