Created
March 3, 2011 02:31
-
-
Save gilmoreorless/852211 to your computer and use it in GitHub Desktop.
Extra validation methods for the jQuery Validate plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** 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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.