Validate phone number with JavaScript

Castro Roy picture Castro Roy · Dec 2, 2010 · Viewed 453.2k times · Source

I found this code in some website, and it works perfectly. It validates that the phone number is in one of these formats:
(123) 456-7890 or 123-456-7890

The problem is that my client (I don't know why, maybe client stuffs) wants to add another format, the ten numbers consecutively, something like this: 1234567890.

I'm using this regular expression,

/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/

How can I add that it also validates the another format? I'm not good with regular expressions.

Answer

EeeeeK picture EeeeeK · Apr 21, 2015

My regex of choice is:

/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im

Valid formats:

(123) 456-7890
(123)456-7890
123-456-7890
123.456.7890
1234567890
+31636363634
075-63546725