Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}
like image 843
Rahbee Alvee Avatar asked Sep 05 '25 09:09

Rahbee Alvee


1 Answers

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})

like image 59
Binary Brain Avatar answered Sep 08 '25 10:09

Binary Brain