Validate Phone Numbers ( with Country Code extension) using Regular Expression
Given some Phone Numbers, the task is to check if they are valid or not using regular expressions. Rules for the valid phone numbers are:
- 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.
Examples:
Input:+91 (976) 006-4000
Output: True
Input: +403 58 59594
Output: True
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^[+]{1}(?:[0-9\-\(\)\/\.]\s?){6, 15}[0-9]{1}$”
Where,
^ : start of the string
- [+]{1} :Matches a “+” character, matches exactly one of the preceding item
- (?:): :Groups multiple tokens together without creating a capture group.
- [0-9\-\(\)\/\.] : matches any character in the set from 0 to 9, “-“, “(“, “)”, “/”, and “.” .
- \\s : match a white space character
- ? : matches 0 or 1 of the preceding item.
- {6, 14} : This expression will match 6 to 14 of the preceding item.
- [0-9] : This will match values from 0 to 9
- {1} : This expression will match exactly one of the preceding item.
- $ : End of the string.
Follow the below steps to implement the idea:
- Create a regex expression for phone numbers.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Phone Number is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidPhoneNumber(string phonenumber)
{
const regex pattern( "^[+]{1}(?:[0-9\\-\\(\\)\\/"
"\\.]\\s?){6,15}[0-9]{1}$" );
if (phonenumber.empty()) {
return "false" ;
}
if (regex_match(phonenumber, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "+919136812895" ;
cout << isValidPhoneNumber(str1) << endl;
string str2 = "+91 9136812895" ;
cout << isValidPhoneNumber(str2) << endl;
string str3 = "+123 123456" ;
cout << isValidPhoneNumber(str3) << endl;
string str4 = "654294563" ;
cout << isValidPhoneNumber(str4) << endl;
return 0;
}
|
Java
import java.util.regex.*;
public class PhoneNumberValidator {
static String isValidPhoneNumber(String phoneNumber) {
String pattern = "^[+]{1}(?:[0-9\\-\\(\\)\\/" +
"\\.]\\s?){6,15}[0-9]{1}$" ;
if (phoneNumber.isEmpty()) {
return "false" ;
}
if (Pattern.matches(pattern, phoneNumber)) {
return "true" ;
} else {
return "false" ;
}
}
public static void main(String[] args) {
String str1 = "+919136812895" ;
System.out.println(isValidPhoneNumber(str1));
String str2 = "+91 9136812895" ;
System.out.println(isValidPhoneNumber(str2));
String str3 = "+123 123456" ;
System.out.println(isValidPhoneNumber(str3));
String str4 = "654294563" ;
System.out.println(isValidPhoneNumber(str4));
}
}
|
Python3
import re
def is_valid_phone_number(phone_number):
pattern = r "^[+]{1}(?:[0-9\\-\\(\\)\\/" \
"\\.]\\s?){6,15}[0-9]{1}$"
if not phone_number:
return "false"
if re.match(pattern, phone_number):
return "true"
else :
return "false"
str1 = "+919136812895"
print (is_valid_phone_number(str1))
str2 = "+91 9136812895"
print (is_valid_phone_number(str2))
str3 = "+123 123456"
print (is_valid_phone_number(str3))
str4 = "654294563"
print (is_valid_phone_number(str4))
|
C#
using System;
using System.Text.RegularExpressions;
class Program
{
static string IsValidPhoneNumber( string phoneNumber)
{
string pattern = @"^[+]{1}(?:[0-9\\-\\(\\)\\/" +
"\\.]\\s?){6,15}[0-9]{1}$" ;
if ( string .IsNullOrEmpty(phoneNumber))
{
return "false" ;
}
if (Regex.IsMatch(phoneNumber, pattern))
{
return "true" ;
}
else
{
return "false" ;
}
}
static void Main( string [] args)
{
string str1 = "+919136812895" ;
Console.WriteLine(IsValidPhoneNumber(str1));
string str2 = "+91 9136812895" ;
Console.WriteLine(IsValidPhoneNumber(str2));
string str3 = "+123 123456" ;
Console.WriteLine(IsValidPhoneNumber(str3));
string str4 = "654294563" ;
Console.WriteLine(IsValidPhoneNumber(str4));
}
}
|
Javascript
function isValidPhoneNumber(phoneNumber) {
const pattern = /^[+]{1}(?:[0-9\-\\(\\)\\/.]\s?){6,15}[0-9]{1}$/;
if (!phoneNumber) {
return "false" ;
}
if (pattern.test(phoneNumber)) {
return "true" ;
} else {
return "false" ;
}
}
const str1 = "+919136812895" ;
console.log(isValidPhoneNumber(str1));
const str2 = "+91 9136812895" ;
console.log(isValidPhoneNumber(str2));
const str3 = "+123 123456" ;
console.log(isValidPhoneNumber(str3));
const str4 = "654294563" ;
console.log(isValidPhoneNumber(str4));
|
Output
true
true
true
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Last Updated :
27 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...