How to validate CVV number using Regular Expression

Given a 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:

  1. It should have 3 or 4 digit.
  2. It should have digit between 0-9.
  3. It should not have any alphabets and 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.

  1. Get the String.
  2. Create a regular expression to check 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 have 3 or 4 digit.
    • $ represents the ending of the string.
  3. Match the given string with the regular expression. In Java, this can be done by using Pattern.matcher().
  4. Return true if the string matches with the given regular expression, else return false.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to validate
// CVV (Card Verification Value)
// number using regex.
import java.util.regex.*;
class GFG {
  
    // Function to validate
    // CVV (Card Verification Value) number.
    // using regular expression.
    public static boolean
    isValidCVVNumber(String str)
    {
        // Regex to check valid CVV number.
        String regex = "^[0-9]{3, 4}$";
  
        // Compile the ReGex
        Pattern p = Pattern.compile(regex);
  
        // If the string is empty
        // return false
        if (str == null) {
            return false;
        }
  
        // Find match between given string
        // and regular expression
        // uaing Pattern.matcher()
  
        Matcher m = p.matcher(str);
  
        // Return if the string
        // matched the ReGex
        return m.matches();
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // Test Case 1:
        String str1 = "561";
        System.out.println(
            isValidCVVNumber(str1));
  
        // Test Case 2:
        String str2 = "5061";
        System.out.println(
            isValidCVVNumber(str2));
  
        // Test Case 3:
        String str3 = "50614";
        System.out.println(
            isValidCVVNumber(str3));
  
        // Test Case 4:
        String str4 = "5a#1";
        System.out.println(
            isValidCVVNumber(str4));
    }
}

chevron_right


Output:

true
true
false
false

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.