How to validate CVV number using Regular Expression
Last Updated :
21 Dec, 2022
Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression.
The valid CVV (Card Verification Value) number must satisfy the following conditions:
- It should have 3 or 4 digits.
- It should have a digit between 0-9.
- It should not have any alphabet or special characters.
Examples:
Input: str = “561”
Output: true
Explanation:
The given string satisfies all the above mentioned conditions. Therefore, it is a valid CVV (Card Verification Value) number.
Input: str = “50614”
Output: false
Explanation:
The given string has five-digit. Therefore, it is not a valid CVV (Card Verification Value) number.
Input: str = “5a#1”
Output: false
Explanation: The given string has alphabets and special characters. Therefore, it is not a valid CVV (Card Verification Value) 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 CVV (Card Verification Value) number as mentioned below:
regex = "^[0-9]{3, 4}$";
- Where:
- ^ represents the starting of the string.
- [0-9] represents the digit between 0-9.
- {3, 4} represents the string that has 3 or 4 digits.
- $ represents the ending of the string.
- Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
- 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 isValidCVVNumber(string str)
{
const regex pattern("^[0-9]{3,4}$");
if (str.empty())
{
return false;
}
if (regex_match(str, pattern))
{
return true;
}
else
{
return false;
}
}
int main()
{
string str1 = "561";
cout << isValidCVVNumber(str1) << endl;
string str2 = "5061";
cout << isValidCVVNumber(str2) << endl;
string str3 = "50614";
cout << isValidCVVNumber(str3) << endl;
string str4 = "5a#1";
cout << isValidCVVNumber(str4) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidCVVNumber(String str)
{
String regex = "^[0-9]{3,4}$";
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 = "561";
System.out.println(isValidCVVNumber(str1));
String str2 = "5061";
System.out.println(isValidCVVNumber(str2));
String str3 = "50614";
System.out.println(isValidCVVNumber(str3));
String str4 = "5a#1";
System.out.println(isValidCVVNumber(str4));
}
}
|
Python3
import re
def isValidCVVNumber(str):
regex = "^[0-9]{3,4}$"
p = re.compile(regex)
if(str == None):
return False
if(re.search(p, str)):
return True
else:
return False
str1 = "561"
print(isValidCVVNumber(str1))
str2 = "5061"
print(isValidCVVNumber(str2))
str3 = "50614"
print(isValidCVVNumber(str3))
str4 = "5a#1"
print(isValidCVVNumber(str4))
|
C#
using System;
using System.Text.RegularExpressions;
class GFG
{
static void Main(string[] args)
{
string[] str={"561","5061","50614","5a#1"};
foreach(string s in str) {
Console.WriteLine( isValidCVVNumber(s) ? "true" : "false");
}
Console.ReadKey(); }
public static bool isValidCVVNumber(string str)
{
string strRegex = @"^[0-9]{3,4}$";
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return (true);
else
return (false);
}
}
|
Javascript
function isValid_CVV_Number(CVV_Number) {
let regex = new RegExp(/^[0-9]{3,4}$/);
if (CVV_Number == null) {
return "false";
}
if (regex.test(CVV_Number) == true) {
return "true";
}
else {
return "false";
}
}
let str1 = "561";
console.log(isValid_CVV_Number(str1));
let str2 = "5061";
console.log(isValid_CVV_Number(str2));
let str3 = "50614";
console.log(isValid_CVV_Number(str3));
let str4 = "5a#1";
console.log(isValid_CVV_Number(str4));
let str5 = "12071998";
console.log(isValid_CVV_Number(str5));
let str6 = "RAH12071998";
console.log(isValid_CVV_Number(str6));
|
Output
true
true
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Please Login to comment...