How to validate MasterCard number using Regular Expression
Given string str, the task is to check whether the given string is a valid Master Card number or not by using Regular Expression.
The valid Master Card number must satisfy the following conditions.
- It should be 16 digits long.
- It should start with either two digits numbers may range from 51 to 55 or four digits numbers may range from 2221 to 2720.
- In the case of 51 to 55, the next fourteen digits should be any number between 0-9.
- In the case of 2221 to 2720, the next twelve digits should be any number between 0-9.
- It should not contain any alphabet or special characters.
Examples:
Input: str = “5114496353984312”;
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid Master Card number.
Input: str = “5582822410”;
Output: false
Explanation:
The given string has 10 digits. Therefore, it is not a valid Master Card number.
Input: str = “6082822463100051”;
Output: false
Explanation:
The given string does not start with either two digits number may range from 51 to 55 or four digits number may range from 2221 to 2720. Therefore, it is not a valid Master Card number.
Approach: The idea is to use Regular Expression to solve this problem. The following steps can be followed to compute the answer.
- Get the String.
- Create a regular expression to check the valid Master Card number as mentioned below:
regex = “^5[1-5][0-9]{14}|^(222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[0-1]\\d|2720)[0-9]{12}$”;
- Where:
- ^ represents the starting of the string.
- 5[1-5] represents the first two-digit number may range from 51 to 55.
- [0-9]{14} represents the next fourteen digits should be any between 0-9.
- | represents the or.
- ( represents the start of the group.
- 222[1-9] represents the first four-digit number may range from 2221 to 2229.
- | represents the or.
- 22[3-9]\\d represents the first four-digit number that may range from 2230 to 2299.
- | represents the or.
- 2[3-6]\\d{2} represents the first four-digit number may range from 2300 to 2699.
- | represents the or.
- 27[0-1]\\d represents the first four-digit number may range from 2700 to 2719.
- | represents the or.
- 2720 represents the first four-digit may start with 2720.
- ) represents the ending of the group.
- [0-9]{12} represents the next twelve digits should be any between 0-9.
- $ represents the ending of the string.
- Match the given string with the Regular Expression.
In Java, this can be done by using Pattern.matcher()
In C++, this can be done by using regex_match()
- Return true if the string matches with the given regular expression, else return false.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
bool isValidMasterCardNo(string str)
{
const regex pattern( "^5[1-5][0-9]{14}|^(222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[0-1]\\d|2720)[0-9]{12}$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "5114496353984312" ;
cout << isValidMasterCardNo(str1) << endl;
string str2 = "2720822463109651" ;
cout << isValidMasterCardNo(str2) << endl;
string str3 = "5582822410" ;
cout << isValidMasterCardNo(str3) << endl;
string str4 = "6082822463100051" ;
cout << isValidMasterCardNo(str4) << endl;
string str5 = "2221149a635##843" ;
cout << isValidMasterCardNo(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValidMasterCardNo(String str)
{
String regex = "^5[1-5][0-9]{14}|"
+ "^(222[1-9]|22[3-9]\\d|"
+ "2[3-6]\\d{2}|27[0-1]\\d|"
+ "2720)[0-9]{12}$" ;
Pattern p
= Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "5114496353984312" ;
System.out.println(
isValidMasterCardNo(str1));
String str2 = "2720822463109651" ;
System.out.println(
isValidMasterCardNo(str2));
String str3 = "5582822410" ;
System.out.println(
isValidMasterCardNo(str3));
String str4 = "6082822463100051" ;
System.out.println(
isValidMasterCardNo(str4));
String str5 = "2221149a635##843" ;
System.out.println(
isValidMasterCardNo(str5));
}
}
|
Python3
import re
def isValidMasterCardNo( str ):
regex = "^5[1-5][0-9]{14}|" + "^(222[1-9]|22[3-9]\\d|" + "2[3-6]\\d{2}|27[0-1]\\d|" + "2720)[0-9]{12}$"
p = re. compile (regex)
if ( str = = None ):
return False
if (re.search(p, str )):
return True
else :
return False
str1 = "5114496353984312"
print (isValidMasterCardNo(str1))
str2 = "2720822463109651"
print (isValidMasterCardNo(str2))
str3 = "5582822410"
print (isValidMasterCardNo(str3))
str4 = "6082822463100051"
print (isValidMasterCardNo(str4))
str5 = "2221149a635##843"
print (isValidMasterCardNo(str5))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool
isValidMasterCardNo( string str)
{
string regex = "^5[1-5][0-9]{14}|"
+ "^(222[1-9]|22[3-9]\\d|"
+ "2[3-6]\\d{2}|27[0-1]\\d|"
+ "2720)[0-9]{12}$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "5114496353984312" ;
Console.WriteLine(
isValidMasterCardNo(str1));
string str2 = "2720822463109651" ;
Console.WriteLine(
isValidMasterCardNo(str2));
string str3 = "5582822410" ;
Console.WriteLine(
isValidMasterCardNo(str3));
string str4 = "6082822463100051" ;
Console.WriteLine(
isValidMasterCardNo(str4));
string str5 = "2221149a635##843" ;
Console.WriteLine(
isValidMasterCardNo(str5));
}
}
|
Javascript
function isValid_MasterCard_Number(MasterCard_Number) {
let regex = new RegExp(/^5[1-5][0-9]{14}|^(222[1-9]|22[3-9]\\d|2[3-6]\\d{2}|27[0-1]\\d|2720)[0-9]{12}$/);
if (MasterCard_Number == null ) {
return "false" ;
}
if (regex.test(MasterCard_Number) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "5114496353984312" ;
console.log(isValid_MasterCard_Number(str1));
let str2 = "2720822463109651" ;
console.log(isValid_MasterCard_Number(str2));
let str3 = "5582822410" ;
console.log(isValid_MasterCard_Number(str3));
let str4 = "6082822463100051" ;
console.log(isValid_MasterCard_Number(str4));
let str5 = "2221149a635##843" ;
console.log(isValid_MasterCard_Number(str5));
let str6 = "RAH12071998" ;
console.log(isValid_MasterCard_Number(str6));
|
Output:
true
true
false
false
false
Time Complexity: O(N) for each test case, where N is the length of the given string.
Auxiliary Space: O(1)
Last Updated :
20 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...