Using jquery validation:
http://docs.jquery.com/Plugins/Validation
I am also validating each field on blur:
//validate each element on blur
$('input, select, radio, checkbox').blur(function(){
$("#createAccount").validate().element(this);
});
I added an additional method for passwords – alpha, numeric, no spaces/dashes:
//Additional validation methods:
jQuery.validator.addMethod("passwordCheck", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
}, "Invalid characters");
A couple of issues:
I would like to validate the password field on blur for everything but the passwordCheck method. So other methods, such as required, should validate on blur – but if an invalid character is entered, then I want the validator to trigger that method then.
Additionally, when adding methods, is there a way to get the error message into the messages portion of the validation call (just to keep them all in one place)?
This is what I did to accomplish this:
For an input that needed to be ignored until it had a value I added a class to the input – “ignore”
In the function, if an input has the class ignore and no value has been entered, then I remove the valid class I set in the validate call. When a value is entered, the normal validations will trigger on those items.