Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7571

Regular Expression for credit card verification

I need to verify a credit card (dummy - i'm not doing any transaction in real). The conditions are

  1. It has to be between 10 and 16 digits.

  2. Start with a digit 3 or 4

  3. Only digits are allowed i mean [0-9].

I tried it and mine was [3-4][0-9]{10-16}. But it seems to be not yielding results.

Thank you in anticipation

Upvotes: 1

Views: 2517

Answers (2)

Timothy Lee Russell
Timothy Lee Russell

Reputation: 3748

You may want to determine if the credit card number does more than just meet the format of the RegEx by using some more aggressive validation.

You could use the Luhn algorithm to verify the number or even use further validation to tell you what company the credit card number belongs to.

Upvotes: 1

Kobi
Kobi

Reputation: 138017

Should be:

^[34][0-9]{9,15}$
  • Start and end anchors are important, or the regex might match only a part from the whole string (for example. 16 digits out of 20)
  • [34] one of the characters. [3-4] is the same.
  • {9,15} is the proper syntax. Note that [34] is the first digit, so you need to subtract one.

Some Examples: http://www.rubular.com/r/q61cPPYW7E

Upvotes: 12

Related Questions