开发者

Loose telephone regex validation

开发者 https://www.devze.com 2023-01-09 08:16 出处:网络
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

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 charact开发者_如何学JAVAers. 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 :)


^[\+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)


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.


Using this as my data, +44 (0) 20 8874 1932

When faced with a non-valid format like +44 (0) 20 8874 1932 simply removing the brackets leads to the non-valid number +44 020 8874 1932.

This number should be presented as +44 20 8874 1932 for international use and as (020) 8874 1932 for national use.

The international format does not include brackets.

0

精彩评论

暂无评论...
验证码 换一张
取 消