简体   繁体   中英

Requires regex for US Phone Format

I need a regex that will match the US phone in a slightly different manner. Actually, on my page i have textbox for taking input, there i had place regular expression validator and the regex to validate the zip code in 999-999-9999 . Till here everything was fine. Later on we added MaskedEditor extender to avoid manually adding - in between the codes of phone number. Also the ClearMaskOnLostFocus is set to false. So every time we have ___-___-____ in our textbox. And my validator is now always validating its against invalid input.

So finally i would like say, i need a regex that will match if my phone is a valid US phone and also passes ___-___-____ , instead of the two it will break..

[Existing Regex]

^[0-9]{3}[\-]{1}[0-9]{3}[\-]{1}[0-9]{4}$

In Javascript - here:

  /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;  

It came from google... http://www.zparacha.com/phone_number_regex/

Here it is part by part:

/^      = match must start with - no chars before
\(?     = optional left brace
(\d{3}) = 3 digits
\)?     = optional right brace
[- ]?   = optional hyphen or space
(\d{3}) = 3 digits
[- ]?   = optional hyphen or space
(\d{4}) = 4 digits
$/      = match must end here - no chars after

只需使用以下内容:

(<your current regex>)|(___-___-____)

PhoneFormat.com has a javascript library that will take whatever number you throw at it, in whatever format you have, and properly format it. If you pass ___-___-____ or 999-999-9999 it will handle it just fine.

Try to look at libphonenumber for inspiration or use. It has most stuff you can need regarding this.

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