Skip to content

Instantly share code, notes, and snippets.

@macinnir
Created August 31, 2012 20:22
Show Gist options
  • Save macinnir/3558443 to your computer and use it in GitHub Desktop.
Save macinnir/3558443 to your computer and use it in GitHub Desktop.
General application of JQuery Validate
/* Method for validating Zip Code */
/* Requires Jquery-latest and Jquery-Validation plugin (http://docs.jquery.com/Plugins/Validation) */
$.validator.addMethod('client_zip_cb',
function(value){
/* Apply validation code */
return value == '99999'
},
'Please enter 99999 for the zip code!'
);
$('#newClientForm').validate({
/* debug : true, */
submitHandler : function(form){
/*
Because checking for a unique email is an ajax action, I applied it after the form has validated successfully. This could, however, be used called with the blur event of the input element, to notify that the email is not unique.
*/
email = $('input[name="client_email"]');
$.ajax({ url : '/email_exists', data : { email : email } , response : function(r){
alert(r);
if(r.unique == 'true'){
/* Finish the submission of the form */
} else {
/* notify the user of the error */
}
}
});
}
, invalidHandler : function(e, validator){
var errors = validator.numberOfInvalids();
if(errors){
var message = "You missed " + errors + " field"
+ (errors == 1 ? "" : "s");
$('#notify').html('<li>' + message + '</li>').show();
} else {
$('#notify').hide();
}
}
/* , ignore : 'input' */
, rules : {
client_zip : "client_zip_cb"
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment