Given a string str, the task is to check whether the given string is a valid Visa Card number or not by using Regular Expression.
The valid Visa Card number must satisfy the following conditions:
- It should be 13 or 16 digits long, new cards have 16 digits and old cards have 13 digits.
- It should start with 4.
- If the cards have 13 digits the next twelve digits should be any number between 0-9.
- If the cards have 16 digits the next fifteen digits should be any number between 0-9.
- It should not contain any alphabet or special characters.
Examples:
Input: str = “4155279860457”;
Output: true
Explanation: The given string satisfies all the above mentioned conditions. Therefore it is a valid Visa Card number.
Input: str = “4155279”;
Output: false.
Explanation: The given string has 7 digits. Therefore it is not a valid Visa Card number.
Input: str = “6155279860457”;
Output: false.
Explanation: The given string doesn’t starts with 4. Therefore it is not a valid Visa 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 valid Visa Card number as mentioned below:
regex = "^4[0-9]{12}(?:[0-9]{3})?$";
- Where:
- ^ represents the starting of the string.
- 4 represents the string that should start with 4.
- [0-9]{12} represents the next twelve digits should be any between 0-9.
- ( represents the start of the group.
- ? represents the 0 or 1 time.
- [0-9]{3} represents the next three digits should be any between 0-9.
- ) represents the ending of the group.
- ? represents the 0 or 1 time.
- $ 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 isValidVisaCardNo(string str)
{
const regex pattern( "^4[0-9]{12}(?:[0-9]{3})?$" );
if (str.empty())
{
return false ;
}
if (regex_match(str, pattern))
{
return true ;
}
else
{
return false ;
}
}
int main()
{
string str1 = "4155279860457" ;
cout << isValidVisaCardNo(str1) << endl;
string str2 = "4155279860457201" ;
cout << isValidVisaCardNo(str2) << endl;
string str3 = "4155279" ;
cout << isValidVisaCardNo(str3) << endl;
string str4 = "6155279860457" ;
cout << isValidVisaCardNo(str4) << endl;
string str5 = "415a27##60457" ;
cout << isValidVisaCardNo(str5) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean
isValidVisaCardNo(String str)
{
String regex = "^4[0-9]{12}(?:[0-9]{3})?$" ;
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 = "4155279860457" ;
System.out.println(
isValidVisaCardNo(str1));
String str2 = "4155279860457201" ;
System.out.println(
isValidVisaCardNo(str2));
String str3 = "4155279" ;
System.out.println(
isValidVisaCardNo(str3));
String str4 = "6155279860457" ;
System.out.println(
isValidVisaCardNo(str4));
String str5 = "415a27##60457" ;
System.out.println(
isValidVisaCardNo(str5));
}
}
|
Python3
import re
def isValidVisaCardNo(string):
regex = "^4[0-9]{12}(?:[0-9]{3})?$" ;
p = re. compile (regex);
if (string = = ''):
return False ;
m = re.match(p, string);
if m is None :
return False
else :
return True
if __name__ = = "__main__" :
str1 = "4155279860457" ;
print (isValidVisaCardNo(str1));
str2 = "4155279860457201" ;
print (isValidVisaCardNo(str2));
str3 = "4155279" ;
print (isValidVisaCardNo(str3));
str4 = "6155279860457" ;
print (isValidVisaCardNo(str4));
str5 = "415a27##60457" ;
print (isValidVisaCardNo(str5));
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static bool
isValidVisaCardNo( string str)
{
string regex = "^4[0-9]{12}(?:[0-9]{3})?$" ;
Regex p = new Regex(regex);
if (str == null ) {
return false ;
}
Match m = p.Match(str);
return m.Success;
}
public static void Main()
{
string str1 = "4155279860457" ;
Console.WriteLine(
isValidVisaCardNo(str1));
string str2 = "4155279860457201" ;
Console.WriteLine(
isValidVisaCardNo(str2));
string str3 = "4155279" ;
Console.WriteLine(
isValidVisaCardNo(str3));
string str4 = "6155279860457" ;
Console.WriteLine(
isValidVisaCardNo(str4));
string str5 = "415a27##60457" ;
Console.WriteLine(
isValidVisaCardNo(str5));
}
}
|
Javascript
function isValid_VisaCard_Number(VisaCard_Number) {
let regex = new RegExp(/^4[0-9]{12}(?:[0-9]{3})?$/);
if (VisaCard_Number == null ) {
return "false" ;
}
if (regex.test(VisaCard_Number) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "4155279860457" ;
console.log(isValid_VisaCard_Number(str1));
let str2 = "4155279860457201" ;
console.log(isValid_VisaCard_Number(str2));
let str3 = "4155279" ;
console.log(isValid_VisaCard_Number(str3));
let str4 = "6155279860457" ;
console.log(isValid_VisaCard_Number(str4));
let str5 = "415a27##60457" ;
console.log(isValid_VisaCard_Number(str5));
let str6 = "RAH12071998" ;
console.log(isValid_VisaCard_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)