Published on

Regex for CVV code validation

Regex for CVV code validation

CVV stands for Card Verfication Value or its also known as CSC which stands for Card Security Code, is a series of numbers that is printed and available mostly on all credit or debit cards. CVV is used as an additional security step when the physical card is not present in transactions. In this article let's understand how we can create a regex for CVV number of a card and how regex can be matched for CVV code.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

Structure of CVV or CSC Code

  • It should have 3 or 4 digits.
  • It should have a digit between 0-9.
  • It should not have any alphabet or special characters.

Regex for checking if CVV or CSC code is valid

Regular Expression-


Test string examples for the above regex-

Input StringMatch Output
33does not match
34212does not match

Here is a detailed explanation of the above regex-


^ asserts position at start of a line
Match a single character present in the list below [0-9]
{3,4} matches the previous token between 3 and 4 times, as many times as possible, giving back as needed (greedy)
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
$ asserts position at the end of a line
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to match CVV Code of card regex pattern.