Lethal Left Eye Lethal Left Eye - 3 months ago 13
Javascript Question

JavaScript Validate Phone Number Using Regex

Greetings overflowers,

I'm trying to write a regular expression to validate phone numbers of the form ########## (10 digits)
i.e. this is these are cases that would be valid: 1231231234 or 1111111111. Invalid cases would be strings of digits that are less than 10 digits or more than 10 digits.

The expression that I have so far is this:
"\d{10}"

Unfortunately, it does not properly validate if the string is 11+ digits long.

Does anyone know of an expression to achieve this task?

Answer

You need to use ancors, i.e.

/^\d{10}$/