Reg exp for phone number

E

Erik Jälevik

I have a regular expression which I believe is correct but it's not behaving
as expected. It is as follows:

/\+?\d+[-\s\d]*\d+/

The idea is that a phone number is allowed to be of the form: optional +
followed by one or more digits followed by any combination of hyphens,
spaces and digits followed by at least one digit.

However, this regexp is letting through all sorts of expressions as long as
they've got two adjacent digits it seems.

Grateful for help!

Thanks,
Erik
 
E

Evertjan.

Erik Jälevik wrote on 06 nov 2003 in comp.lang.javascript:
I have a regular expression which I believe is correct but it's not
behaving as expected. It is as follows:

/\+?\d+[-\s\d]*\d+/

The idea is that a phone number is allowed to be of the form: optional
+ followed by one or more digits followed by any combination of
hyphens, spaces and digits followed by at least one digit.

However, this regexp is letting through all sorts of expressions as
long as they've got two adjacent digits it seems.

The expression should have begin and end markers:

/^\+?\d+[-\s\d]*\d+$/

even simpler:

/^\+?\d[-\s\d]*\d$/

mind the \s also matches other white space types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top