I\'m having issues with the jquery validator validating phone numbers. So in a form where I have an input for phone number, I use jquery\'s additional methods to validate the ph
Here is a custom validation for phone template:
jQuery.validator.addMethod("phone", function (phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^\(?[\d\s]{3}-[\d\s]{3}-[\d\s]{4}$/);
}, "Invalid phone number");
ex: 123-123-1234
And here is a format with country prefix :
jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^\+[0-9]{11}$/);
}, "Please specify a valid phone number");
Just add the phoneUS class to the imput ex +41748987145
This method is an extension for jquery validation. In this way you define your own custom rules. Belive me, there are situations where you need them.