Function isValidCreditCard (type, ccnum ){ If (type = "Visa "){ // Visa: length 16, prefix 4, dashes optional. Var re =/^ 4 \ d {3 }-? \ D {4 }-? \ D {4 }-? \ D {4} $ /; } Else if (type = "MC "){ // Mastercard: length 16, prefix 51-55, dashes optional. Var re =/^ 5 [1-5] \ d {2 }-? \ D {4 }-? \ D {4 }-? \ D {4} $ /; } Else if (type = "Disc "){ // Discover: length 16, prefix 6011, dashes optional. Var re =/^ 6011 -? \ D {4 }-? \ D {4 }-? \ D {4} $ /; } Else if (type = "AmEx "){ // American Express: length 15, prefix 34 or 37. Var re =/^ 3 [4, 7] \ d {13} $ /; } Else if (type = "Diners "){ // Diners: length 14, prefix 30, 36, or 38. Var re =/^ 3 [0, 6, 8] \ d {12} $ /; } If (! Re. test (ccnum) return false; // Remove all dashes for the checksum // Checks to eliminate negative numbers Ccnum = ccnum. split ("-"). join (""); // Checksum ("Mod 10 ") // Add even digits in even length strings // Or odd digits in odd length strings. Var checksum = 0; For (var I = (2-(ccnum. length % 2); I <= ccnum. length; I + = 2 ){ Checksum + = parseInt (ccnum. charAt (I-1 )); } // Analyze odd digits in even length strings // Or even digits in odd length strings. For (var I = (ccnum. length % 2) + 1; I <ccnum. length; I + = 2 ){ Var digit = parseInt (ccnum. charAt (I-1) * 2; If (digit <10) {checksum + = digit ;} Else {checksum + = (digit-9 );} } If (checksum % 10) = 0) return true; else return false; } |