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
It has to be between 10 and 16 digits.
Start with a digit 3 or 4
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.
精彩评论