Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8729026

Browse files
committed
merged branch ricardclau/improve-creditcard-regexp (PR#6583)
This PR was merged into the master branch.Commits-------5be0042 better regexp, more test cases, added comments about each credit cardcc278af [Validator] Fix `CardSchemeValidator` double violation when value is non-numeric. Making scheme option accept strings in addition to arrays.Discussion----------[Validator] Improve regexp for Credit Cards and some more testsBug fix: yesFeature addition: noBackwards compatibility break: noSymfony2 tests pass: yesFixes the following tickets:Todo: Ensure these regexps are proper (credit card validation is always a pain)License of the code: MITDocumentation PR:Regarding Cases excluded from new Regular Expressions:- Credit card lengths should be respected, these regexp cover lengths inhttp://en.wikipedia.org/wiki/Bank_card_number- Visa length can only be 16 and 13 (older ones)- Diners Cards starting by 5 come from a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard (according tohttp://www.regular-expressions.info/creditcard.html).- There seems to be JCB cards starting by 2131 and 1800, I could find them is some places, also found these numbers being tested in Credit Card generators, but some people don't cover them. I don't know their story eitherAny comments will be much appreciated!---------------------------------------------------------------------------by fabpot at 2013-01-06T19:33:27ZThanks for working on this. It would be very valuable if you can add information about these regexes as comments (with links to relevant sources -- like what you've done in the PR description). Thanks.---------------------------------------------------------------------------by ricardclau at 2013-01-06T21:01:52ZAlways glad to be able to contribute a little bit@fabpot you mean@link /@see PHPDoc inside CardSchemeValidator.php? Or further comments in this discussion before adding them?---------------------------------------------------------------------------by fabpot at 2013-01-06T21:16:48ZThe more information we can add in the class, the better it is.---------------------------------------------------------------------------by ricardclau at 2013-01-07T20:56:05ZI've added comments and included code from#6603 as I've said there. If you need something else, please let me know, once this is merged,#6603 can also be closed---------------------------------------------------------------------------by fabpot at 2013-01-07T21:41:40ZCan you keep the commit from#6603 to keep ownership?---------------------------------------------------------------------------by ricardclau at 2013-01-07T21:44:16ZI actually have thought about that... let me try my git skills :)---------------------------------------------------------------------------by ricardclau at 2013-01-07T21:59:16ZThere you go!
2 parentsb378964 +5be0042 commit8729026

File tree

2 files changed

+108
-18
lines changed

2 files changed

+108
-18
lines changed

‎src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php‎

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,80 @@
1818
* Validates that a card number belongs to a specified scheme.
1919
*
2020
* @see http://en.wikipedia.org/wiki/Bank_card_number
21+
* @see http://www.regular-expressions.info/creditcard.html
2122
* @author Tim Nagel <t.nagel@infinite.net.au>
2223
*/
2324
class CardSchemeValidatorextends ConstraintValidator
2425
{
2526
protected$schemes =array(
27+
/**
28+
* American Express card numbers start with 34 or 37 and have 15 digits.
29+
*/
2630
'AMEX' =>array(
27-
'/^(3[47])([0-9]{13})/'
31+
'/^3[47][0-9]{13}$/'
2832
),
33+
/**
34+
* China UnionPay cards start with 62 and have between 16 and 19 digits.
35+
* Please note that these cards do not follow Luhn Algorithm as a checksum.
36+
*/
2937
'CHINA_UNIONPAY' =>array(
30-
'/^(62)([0-9]{16,19}/'
38+
'/^62[0-9]{14,17}$/'
3139
),
40+
/**
41+
* Diners Club card numbers begin with 300 through 305, 36 or 38. All have 14 digits.
42+
* There are Diners Club cards that begin with 5 and have 16 digits.
43+
* These are a joint venture between Diners Club and MasterCard, and should be processed like a MasterCard.
44+
*/
3245
'DINERS' =>array(
33-
'/^(36)([0-9]{12})/',
34-
'/^(30[0-5])([0-9]{11})/',
35-
'/^(5[45])([0-9]{14})/'
46+
'/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/',
3647
),
48+
/**
49+
* Discover card numbers begin with 6011, 622126 through 622925, 644 through 649 or 65.
50+
* All have 16 digits
51+
*/
3752
'DISCOVER' =>array(
38-
'/^(6011)([0-9]{12})/',
39-
'/^(64[4-9])([0-9]{13})/',
40-
'/^(65)([0-9]{14})/',
41-
'/^(622)(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])([0-9]{10})/'
53+
'/^6011[0-9]{12}$/',
54+
'/^64[4-9][0-9]{13}$/',
55+
'/^65[0-9]{14}$/',
56+
'/^622(12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|91[0-9]|92[0-5])[0-9]{10}$/'
4257
),
58+
/**
59+
* InstaPayment cards begin with 637 through 639 and have 16 digits
60+
*/
4361
'INSTAPAYMENT' =>array(
44-
'/^(63[7-9])([0-9]{13})/'
62+
'/^63[7-9][0-9]{13}$/'
4563
),
64+
/**
65+
* JCB cards beginning with 2131 or 1800 have 15 digits.
66+
* JCB cards beginning with 35 have 16 digits.
67+
*/
4668
'JCB' =>array(
47-
'/^(352[8-9]|35[3-8][0-9])([0-9]{12})/'
69+
'/^(?:2131|1800|35[0-9]{3})[0-9]{11}$/'
4870
),
71+
/**
72+
* Laser cards begin with either 6304, 6706, 6709 or 6771 and have between 16 and 19 digits
73+
*/
4974
'LASER' =>array(
50-
'/^(6304|670[69]|6771)([0-9]{12,15})/'
75+
'/^(6304|670[69]|6771)[0-9]{12,15}$/'
5176
),
77+
/**
78+
* Maestro cards begin with either 5018, 5020, 5038, 5893, 6304, 6759, 6761, 6762, 6763 or 0604
79+
* They have between 12 and 19 digits
80+
*/
5281
'MAESTRO' =>array(
53-
'/^(5018|5020|5038|6304|6759|6761|676[23]|0604)([0-9]{8,15})/'
82+
'/^(5018|5020|5038|6304|6759|6761|676[23]|0604)[0-9]{8,15}$/'
5483
),
84+
/**
85+
* All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
86+
*/
5587
'MASTERCARD' =>array(
56-
'/^(5[1-5])([0-9]{14})/'
88+
'/^5[1-5][0-9]{14}$/'
5789
),
90+
/**
91+
* All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
92+
*/
5893
'VISA' =>array(
59-
'/^(4)([0-9]{12})/'
94+
'/^4([0-9]{12}|[0-9]{15})$/'
6095
),
6196
);
6297

@@ -74,9 +109,11 @@ public function validate($value, Constraint $constraint)
74109

75110
if (!is_numeric($value)) {
76111
$this->context->addViolation($constraint->message);
112+
113+
return;
77114
}
78115

79-
$schemes =array_flip($constraint->schemes);
116+
$schemes =array_flip((array)$constraint->schemes);
80117
$schemeRegexes =array_intersect_key($this->schemes,$schemes);
81118

82119
foreach ($schemeRegexesas$regexes) {

‎src/Symfony/Component/Validator/Tests/Constraints/CardSchemeValidatorTest.php‎

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,79 @@ public function testValidNumbers($scheme, $number)
5656
$this->context->expects($this->never())
5757
->method('addViolation');
5858

59-
$this->validator->validate($number,newCardScheme(array('schemes' =>array($scheme))));
59+
$this->validator->validate($number,newCardScheme(array('schemes' =>$scheme)));
60+
}
61+
62+
/**
63+
* @dataProvider getInvalidNumbers
64+
*/
65+
publicfunctiontestInvalidNumbers($scheme,$number)
66+
{
67+
$this->context->expects($this->once())
68+
->method('addViolation');
69+
70+
$this->validator->validate($number,newCardScheme(array('schemes' =>$scheme)));
6071
}
6172

6273
publicfunctiongetValidNumbers()
6374
{
6475
returnarray(
65-
array('VISA','42424242424242424242'),
6676
array('AMEX','378282246310005'),
6777
array('AMEX','371449635398431'),
6878
array('AMEX','378734493671000'),
79+
array('AMEX','347298508610146'),
80+
array('CHINA_UNIONPAY','6228888888888888'),
81+
array('CHINA_UNIONPAY','62288888888888888'),
82+
array('CHINA_UNIONPAY','622888888888888888'),
83+
array('CHINA_UNIONPAY','6228888888888888888'),
6984
array('DINERS','30569309025904'),
85+
array('DINERS','36088894118515'),
86+
array('DINERS','38520000023237'),
7087
array('DISCOVER','6011111111111117'),
7188
array('DISCOVER','6011000990139424'),
89+
array('INSTAPAYMENT','6372476031350068'),
90+
array('INSTAPAYMENT','6385537775789749'),
91+
array('INSTAPAYMENT','6393440808445746'),
7292
array('JCB','3530111333300000'),
7393
array('JCB','3566002020360505'),
94+
array('JCB','213112345678901'),
95+
array('JCB','180012345678901'),
96+
array('LASER','6304678107004080'),
97+
array('LASER','6706440607428128629'),
98+
array('LASER','6771656738314582216'),
99+
array('MAESTRO','6759744069209'),
100+
array('MAESTRO','5020507657408074712'),
101+
array('MAESTRO','6759744069209'),
102+
array('MAESTRO','6759744069209'),
74103
array('MASTERCARD','5555555555554444'),
75104
array('MASTERCARD','5105105105105100'),
76105
array('VISA','4111111111111111'),
77106
array('VISA','4012888888881881'),
78107
array('VISA','4222222222222'),
108+
array(array('AMEX','VISA'),'4111111111111111'),
109+
array(array('AMEX','VISA'),'378282246310005'),
110+
array(array('JCB','MASTERCARD'),'5105105105105100'),
111+
array(array('VISA','MASTERCARD'),'5105105105105100'),
112+
);
113+
}
114+
115+
publicfunctiongetInvalidNumbers()
116+
{
117+
returnarray(
118+
array('VISA','42424242424242424242'),
119+
array('AMEX','357298508610146'),
120+
array('DINERS','31569309025904'),
121+
array('DINERS','37088894118515'),
122+
array('INSTAPAYMENT','6313440808445746'),
123+
array('CHINA_UNIONPAY','622888888888888'),
124+
array('CHINA_UNIONPAY','62288888888888888888'),
125+
array('AMEX','30569309025904'),// DINERS number
126+
array('AMEX','invalid'),// A string
127+
array('AMEX',0),// a lone number
128+
array('AMEX','0'),// a lone number
129+
array('AMEX','000000000000'),// a lone number
130+
array('DINERS','3056930'),// only first part of the number
131+
array('DISCOVER','1117'),// only last 4 digits
79132
);
80133
}
81134
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp