开发者

Regular Expression for credit card verification

开发者 https://www.devze.com 2023-02-18 06:55 出处:网络
I need to verify a credit card (dummy - i\'m not doing any transaction in real). The conditions are It has to be between 10 and 16 digits.

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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号