Skip to content

Commit

Permalink
Restricting credit card validator to include length check. Closes gh-772
Browse files Browse the repository at this point in the history


Without this, '41111' is considered a valid credit card number.
  • Loading branch information
AndrewRayCode authored and jzaefferer committed Jun 25, 2013
1 parent 4bf813b commit f5f47c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,12 @@ $.extend($.validator, {

value = value.replace(/\D/g, "");

// Basing min and max length on
// http://developer.ean.com/general_info/Valid_Credit_Card_Types
if ( value.length < 13 || value.length > 19 ) {
return false;
}

for (var n = value.length - 1; n >= 0; n--) {
var cDigit = value.charAt(n);
nDigit = parseInt(cDigit, 10);
Expand Down
5 changes: 3 additions & 2 deletions test/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ test("equalTo", function() {

test("creditcard", function() {
var method = methodTest("creditcard");
ok( method( "446-667-651" ), "Valid creditcard number" );
ok( method( "446 667 651" ), "Valid creditcard number" );
ok( method( "4111-1111-1111-1111" ), "Valid creditcard number" );
ok( method( "4111 1111 1111 1111" ), "Valid creditcard number" );
ok(!method( "41111" ), "Invalid creditcard number" );
ok(!method( "asdf" ), "Invalid creditcard number" );
});

Expand Down

0 comments on commit f5f47c5

Please sign in to comment.