Phone numbers and Mobile Numbers around the world usually varies from 7 to 15 digits, depending on which country you are in and which phone provider you are subscribed to. In this article let’s understand how we can create a regex for Phone Number and how regex can be matched for Phone Number validation.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of Mobile Phone Number with Country Code
- The numbers should start with a plus sign ( + )
- It should be followed by Country code and National number.
- It may contain white spaces or a hyphen ( – ).
- The length of phone numbers may vary from 7 digits to 15 digits.
Regex for checking if Mobile Phone Number is valid or not
Regular Expression-
/^[+]{1}(?:[0-9\-\(\)\/\.]\s?){6, 15}[0-9]{1}$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
+223-2343-17-2-83924 | does not match |
+919765424120 | matches |
s333asd212 | does not match |
+133 65 435654 | matches |
+91 (244) 3424230 | matches |
Here is a detailed explanation of the above regex-
/^[+]{1}(?:[0-9\-\(\)\/\.]\s?){6, 15}[0-9]{1}$/gm
^ asserts position at start of a line
Match a single character present in the list below [+]
{1} matches the previous token exactly one time (meaningless quantifier)
+ matches the character + with index 4310 (2B16 or 538) literally (case sensitive)
Non-capturing group (?:[0-9\-\(\)\/\.]\s?){6,15}
{6,15} matches the previous token between 6 and 15 times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [0-9\-\(\)\/\.]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
\- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
\( matches the character ( with index 4010 (2816 or 508) literally (case sensitive)
\) matches the character ) with index 4110 (2916 or 518) literally (case sensitive)
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\s matches any whitespace character (equivalent to [\r\n\t\f\v ])
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [0-9]
{1} matches the previous token exactly one time (meaningless quantifier)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Hope this article was useful to match Mobile Phone Number regex pattern. In a world where phone numbers vary from 7 to 15 digits based on location and provider, understanding regex for phone number validation is crucial. Regex empowers text search, manipulation, and input validation. The article explored the structure of mobile numbers, shared a regex pattern for validation, and provided insightful examples. Mastering regex is a valuable skill for effective data handling and user input verification.