Skip to content Skip to sidebar Skip to footer

RegEx For Phone Number In JavaScript

I was using RegEx to validate user phone numbers. I have set of requirements for phone number validation. I dont have much idea about the RegEX. can anyone help me to provide a mat

Solution 1:

Based on your samples, try this:

^(?:\+?\d{2}[ -]?\d{3}[ -]?\d{5}|\d{4})$

It will match all the correct values.

DESCRIPTION:

Regular expression visualization

DEMO:

http://regexr.com?340nf


Solution 2:

Just to help you build some concepts.
The following regex would match the first seven inputs you provided.

/^\+?\d{2}[- ]?\d{3}[- ]?\d{5}$/

\+? would match a + sign. The ? in it makes the + sign optional.
\d{2} matches two digits
[- ]? matches either a - or a (space). ? makes the occurrence of - or (space) optional.
\d{5} then matches 5 digits.
^ and $ are the start and end anchors.


Solution 3:

based on you samples try this pattern.

^(?:\+?\d{2}[ -]?[\d -][\d -]+)$

Post a Comment for "RegEx For Phone Number In JavaScript"