Skip to content

Instantly share code, notes, and snippets.

@gilmoreorless
Created March 3, 2011 02:31
Show Gist options
  • Save gilmoreorless/852211 to your computer and use it in GitHub Desktop.
Save gilmoreorless/852211 to your computer and use it in GitHub Desktop.
Extra validation methods for the jQuery Validate plugin
/*** Allow digits and whitespace ***/
// Method 1:
jQuery.validator.addMethod("digitsWhitespace", function(value, element) {
return this.optional(element) || /^[\d\s]+$/.test(value);
}, jQuery.validator.messages.digits);
// Method 2:
jQuery.validator.addMethod("digitsWhitespace", function(value, element) {
return jQuery.validator.methods.digits(value.replace(/\s/g, ''), element);
}, jQuery.validator.messages.digits);
/*** Australian phone number formats ***/
// Australian phone number
jQuery.validator.addMethod("phoneAU", function(value, element) {
return this.optional(element) || /^(\+?61|0)\d{9}$/.test(value.replace(/\s+/g, ""));
}, "Please specify a valid phone number");
// Australian mobile phone number
jQuery.validator.addMethod("mobileAU", function(value, element) {
return this.optional(element) || /^(\+?61|0)4\d{8}$/.test(value.replace(/\s+/g, ""));
}, "Please specify a valid mobile phone number");
@gilmoreorless
Copy link
Author

I'm going to be honest with you here - I wrote these a few years ago and put them online quite a while later, as a reference for someone I used to work with.

I have absolutely no idea if they still work with the latest version of jQuery Validate as I haven't used it in over a year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment