tags:

views:

27

answers:

2

I've been trying to find a validation regex to do some loose validation on phone numbers. I'm going to strip some of the stuff out when I use them, but I would like to allow the user the freedom to enter their number as they want, and I want to display it as they have entered it.

I figured that the best thing to do is whitelist my characters. I figured on

[space] + - [0-9] ( )

Are there any other characters that I should be allowing? I'm not quite sure if I should be looking for characters which do not match this in the pattern?

So far all I can come up with is,

[\+0-9\s\-\(\)]

Which seems to match every character

I've been playing around in here, http://gskinner.com/RegExr/

Using this as my data,

+44 787 553 7794
+1-818-923-4821
&9_981-432 p

+44 (0) 20 874 1932

If anyone could nudge me in the right direction, I'd really appreciate it! Thanks :)

A: 

What about keeping the string as the user enters it and then removing everything that is not a number or # or + if you want to dial it.

wilx
Yeah, it's more of a workaround though. I'd ideally like to validate it just in case the user has an error
DavidYell
A: 
^[\+0-9\s\-\(\)]+$

^ and $ Will ensure we are matching the whole string

the + (before the final $) will allow the range to match the whole number (multiple characters)

Brooke
Ah! That's what I was missing! Thanks
DavidYell