Take the 2-minute tour ×

I need an input field to only validate a 10 digit phone number, no more and no less than 10 digits and strip any dashes, etc.

Here is the code so far:

jQuery.validator.addMethod ("phone", function(phone, element) {
if (phone == null) {
return false;
var stripped = phone.replace(/[\s()+-]|ext\.?/gi, "");
// 10 is the minimum number of numbers required
return this.optional(element) || ((/\d{10,10}/i).test(stripped));
}, "*Please enter a valid phone number.");

However, it validates fine if the user enters less than 10 digits, but if they enter more than 10 it doesn't return the error message and they can submit the form.

Any help would be fantastic!


share|improve this question
is that just 10 numbers without any special characters and spaces? –  blasteralfred Ψ Sep 6 '12 at 14:12
add comment

1 Answer

You are checking for 10 digits anywhere in the string, instead, check if the entire string is 10 digits and nothing else:

return this.optional(element) || ((/^\d{10}$/).test(stripped));


^ = start of string

$ = end of string

no need for /i, that would only work on alpha chars.

share|improve this answer
Thanks so much! worked like a charm! –  user1630786 Sep 6 '12 at 14:16
add comment

Your Answer


