Search code examples
javascriptjqueryjquery-validate

How to use jQuery to validate North American 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?

form.validate({

    rules: {

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

Solution

  • Your regex should be something like

    [0-9\-\(\)\s]+.

    It matches numbers, dashes, parentheses and space.

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

    ([0-9]{10})|(\([0-9]{3}\)\s+[0-9]{3}\-[0-9]{4})