This article describes the JavaScript implementation of credit card verification method. Share to everyone for your reference. The specific analysis is as follows:
Here the JavaScript version of the credit card verification code, using the Luhn algorithm
function Isvalidcreditcard (type, ccnum) {if (type = = "Visa") {//Visa:length, prefix 4, dashes optional.
var re =/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
else if (type = = "MC") {//mastercard:length, prefix 51-55, dashes optional.
var re =/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
else if (type = = "Disc") {//discover:length, prefix 6011, dashes optional.
var re =/^6011-?\d{4}-?\d{4}-?\d{4}$/;
else if (type = = "AmEx") {//American express:length, prefix or 37.
var re =/^3[4,7]\d{13}$/;
else if (type = = "Diners") {//diners:length, prefix, 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 ("-"). Join ("");
Checksum ("Mod")//ADD even digits in even length strings//or odd digits in odd length strings.
var checksum = 0; For (Var i= (ccnum.length% 2); I<=ccnum.lenGth
i+=2) {checksum + = parseint (Ccnum.charat (i-1));
}//Analyze odd digits in even length strings//or even the 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 <) {checksum + = digit;}
else {checksum + = (digit-9);} ' If ((checksum%) = = 0) return true;
else return false; }
The
wants this article to help you with your JavaScript programming.