开发者

Credit Card validator for Symfony 1.4

Is anyone using a working Credit Card number validator with Symfony 1.4?

I found https://gist.github.com/181049 via http://brentertainment.com/2009/09/01/some-helpful-symfony-form-validators/ but at least the switch regex pattern appears to be definitely broken, and probably jcb / enroute as well.

I think the problem is that it's using square brackets instead of parentheses where it's trying to match the prefixes, but if that's broken then it leads me to doubt the rest of the validator.

/^[4903|4911|4936|5641|6333|6759|6334|6767开发者_运维问答]\d{12}$/

should be

/^(4903|4911|4936|5641|6333|6759|6334|6767)\d{12}$/

Does anyone have a working, tested validator they're using in production? Preferably with UK cards?


I would stay away from US regexps, that example omits many valid prefixes: 40* for Visa DR, 48* for Visa/Electron, 35* for solo.

Lengths can also span 16-19 digits for most schemes so the length constraint is incorrect.

The BIN prefixes (1st 6) are regularly modified (ranges added/retired/exempted from e-comm) and to a lesser degree the same is true for the 1st 4, so you may want to rely on the 1st digit & a LUHN check instead.

If your in the UK; http://www.barclaycard.co.uk/business/documents/pdfs/bin_rules.pdf


RegexBuddy Library suggests the following:

^(?:4\d{3}[ -]*\d{4}[ -]*\d{4}[ -]*\d(?:\d{3})?|5[1-5]\d{2}[ -]*\d{4}[ -]*\d{4}[ -]*\d{4}|6(?:011|5[0-9]{2})[ -]*\d{4}[ -]*\d{4}[ -]*\d{4}|3[47]\d{2}[ -]*\d{6}[ -]*\d{5}|3(?:0[0-5]|[68][0-9])\d[ -]*\d{6}[ -]*\d{4}|(?:2131|1800)[ -]*\d{6}[ -]*\d{5}|35\d{2}[ -]*\d{4}[ -]*\d{4}[ -]*\d{4})$

The above will match all credit cards such as the following examples:

Visa
4123456789012345
4123 4567 8901 2345
4123-4567-8901-2345

MasterCard
5123456789012345
5123-4567-8901-2345
5123 4567 8901 2345

Discover
6011123456789012
6011-1234-5678-9012
6011 1234 5678 9012

American Express
341234567890123
3412 345678 90123
3412-345678-90123

Diner's Club
30012345678901
3001 234567 8901
3001-234567-8901

JCB
213112345678901
2131 123456 78901
2131-123456-78901
3512345678901234
3512 3456 7890 1234
3512-3456-7890-1234
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜