Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression matching a 3 or 4 digit cvv of a credit card

Tags:

regex

I really don't know regexp yet, but sooner or later I will read and learn. But for now I need a regular expression matching a 3 or 4 digit cvv of a credit card so I can validate the input in javascript and later in PHP. cvv are supposed to be 3 or 4 digit as far as I know.

I tried this in my javascript code and I am not sure if it is correct.

if (/[0-9]{3}+/.test(value)) return false; 
like image 920
themhz Avatar asked Aug 17 '12 19:08

themhz


People also ask

Are CVV codes 3 or 4 digits?

The CVV is a 3 or 4 digit code embossed or imprinted on the reverse side of many credit or debit cards. This is an extra security measure to ensure that you have physical possession of the credit card itself.

Is there a 4 digit CVV code?

Location for American ExpressThe CVC is the four-digit number located on the front of the credit card. The four-digit number is located on the right or left above the account number.

How do you enter a 4 digit CVV number?

Remove the last number of the credit card number, then enter in the 4 digit CVV code, finally put the last digit of the credit card back on the credit card number line.

Is a CVV always 3 numbers?

A CVV is not always 3 digits; it can be 4 digits on some cards. Although most credit cards and debit cards have a 3-digit CVV located on the back of the card, American Express cards are the exception, with a 4-digit CVV on the front. Visa, Mastercard, and Discover cards all use a 3-digit CVV.


1 Answers

To match 3 or 4 digits, use the following regular expression:

/^[0-9]{3,4}$/ 

Explanation:

  • ^: Start of string anchor
  • [0-9]: Digit between 0 and 9 (you could also use \d here)
  • {3,4}: A quantifier meaning between 3 and 4.
  • $: End of string anchor
like image 104
Mark Byers Avatar answered Sep 21 '22 02:09

Mark Byers