jquery to validate phone number


I want to validate phone numbers like (123) 456-7890 or 1234567890 How should be the 'matches' condition be written in the following code?


    rules: {

       phoneNumber: {matches:"[0-9]+",minlength:10, maxlength:10}

This question is tagged with javascript jquery jquery-validate

~ Asked on 2013-11-07 15:48:02

The Best Answer is


Your regex should be something like


It matches numbers, dashes, parentheses and space.

If you need something more strict, matching just your example, try this:


~ Answered on 2013-11-07 15:55:33


Your code:

rules: {
    phoneNumber: {
        matches: "[0-9]+",  // <-- no such method called "matches"!

There is no such callback function, option, method, or rule called matches anywhere within the jQuery Validate plugin. (EDIT: OP failed to mention that matches is his custom method.)

However, within the additional-methods.js file, there are several phone number validation methods you can use. The one called phoneUS should satisfy your pattern. Since the rule already validates the length, minlength and maxlength are redundantly unnecessary. It's also much more comprehensive in that area codes and prefixes can not start with a 1.

rules: {
    phoneNumber: {
        phoneUS: true

DEMO: http://jsfiddle.net/eWhkv/

If, for whatever reason, you just need the regex for use in another method, you can take it from here...

jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, "");
    return this.optional(element) || phone_number.length > 9 && 
}, "Please specify a valid phone number");

~ Answered on 2013-11-07 16:15:19

Most Viewed Questions: