简体   繁体   中英

regex to validate phone number

Help me write a regex for below conditions

  1. number can start with +
  2. number can contain - or . but not () and /
  3. number can start with 0
  4. Min number in the string should be 9 digits excluding extension details and starting +
  5. max number in the phone number field should not reach more than 14 excluding +
  6. if the string contains ex/ext/x then the digit after should not have more than 5 characters (normally 4)

this above should satisfy examples below

0-1234-123456
+91-1234-56789012
+91-1234-56789012 x1234
+91-1234-56789012 ex1234
+91-1234-56789012 ext12345
+91-1234-56789012x1234
+91-1234-56789012ex1234
+91-1234-56789012ext12345
91-1234-56789012
91-1234-56789012 x1234
91-1234-56789012 ex1234
91-1234-56789012 ext12345
91-1234-56789012x1234
91-1234-56789012ex1234
91-1234-56789012ext12345
91123456789012
91123456789012 x1234
91123456789012 ex1234
91123456789012 ext12345
91123456789012x1234
91123456789012ex1234
91123456789012ext12345
91.1234.56789012
91.1234.56789012 x1234
91.1234.56789012 ex1234
91.12345.6789012 ext12345
91.12345.6789012x1234
91.12345.6789012ex1234
91.12345.6789012ext12345
1-234-567-8901
1-234-567-8901 x1234
1-234-567-8901 ext1234
1 234 567-8901
1.234.567.8901
12345678901

I found few links online one of them is http://ericholmes.ca/php-phone-number-validation-revisited/

and on stackoverflow

A comprehensive regex for phone number validation

also

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$

is not working for many of the above

^\+?(\d[.\- ]*){9,14}(e?xt?\d{1,5})?$

Explanation;

  • ^ Asserts start of string
  • \\+? Matches an optional plus
  • (\\d[.\\- ]*){9,14} between 9 and 14 single digits, possibly seperated by spaces, dots, or dashes.
  • (e?xt?\\d{1,5})? Optionally ax possibly preceeded by an e or followed by a t. The letters always followed by between 1 and 5 numbers.
  • $ Asserts end of the string

This will do it, but depending on which language you are programming in (we always need to know that with regexs, so if this doesn't work for you, reply with the language used. I've tested it in PHP5.)

Your condition 5 (max 14 chars in the phone no) appears to be in error, since several of your examples contain 16 characters if they include dots or hyphens. In any case, this does not check for overall length of the whole thing because of the other length checks it does; it would need a second regex, or, better, check the string length beforehand (eg in PHP by doing a call to strlen).

You might want to allow for a space in extension numbers, eg ext 1234; if so add \\s* in the appropriate place.

I hope this helps.

^\+?\d[\d-\.\s]{8,15}\s?((ext|ex|x)\d{3,5})?$

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