简体   繁体   中英

Loose telephone regex validation

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 :)

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

在用户输入字符串时保留该字符串,然后删除所有非数字或#或+(如果要拨号)的字符串,该怎么办?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM