Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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");
@ahmad2x4

This comment has been minimized.

Copy link

@ahmad2x4 ahmad2x4 commented Jan 19, 2012

@Gilmore how does this "optional" method work?
I added {required:false} but didn't work.

@gilmoreorless

This comment has been minimized.

Copy link
Owner Author

@gilmoreorless gilmoreorless commented Jan 20, 2012

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