To use the validation rule simply add the method to the validate plugin using the code below to a place where it is called before form validation.
$.validator.addMethod('phone', function(value) {
var numbers = value.split(/\d/).length - 1;
return (10 <= numbers && numbers <= 20 && value.match(/^(\+){0,1}(\d|\s|\(|\)){10,20}$/)); }, 'Please enter a valid phone number');The validator will allow between 9 and 19 digit numbers with a hard character limit between 10 and 20. The special characters allowed are: '(', ')', '+', and ' '. Play around with it to see just what formats are accepted and tweak to your needs.
2 comments:
thank you ! you helped me much !
Thank you for the great start; however, your code was not restrictive enough for me. I am using this regex instead:
$.validator.addMethod('phone', function(value) {
return (value.match(/^((\+){0,1}\d\s)?\(?([0-9]{3})\)?\s*[\. -]?\s*([0-9]{3})\s*[\. -]?\s*([0-9]{4})\s?((ext|x)\s*\.?:?\s*([0-9]+))?$/)); }, 'Please enter a valid phone number (Intl format accepted + ext: or x:)');
Post a Comment