Regex
regex
December 3, 20223 min read

Regex for Mobile Phone Number validation

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 StringMatch Output
+223-2343-17-2-83924does not match
+919765424120matches
s333asd212does not match
+133 65 435654matches
+91 (244) 3424230matches

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.

Share this blog
Tagged in :
regex
Like what you read?
Subscribe to our Newsletter
Subscribe to our email newsletter and unlock access to members-only content and exclusive updates.
About the Author
Satvik
Satvik
Entrepreneur
Satvik is a passionate developer turned Entrepreneur. He is fascinated by JavaScript, Operating System, Deep Learning, AR/VR. He has published several research papers and applied for patents in the field as well. Satvik is a speaker in conferences, meetups talking about Artificial Intelligence, JavaScript and related subjects. His goal is to solve complex problems that people face with automation. Related projects can be seen at - [Projects](/projects)
View all articles
Previous Article
December 1, 20224 min read
Next Article
December 17, 20224 min read