This article illustrates the method by which JavaScript randomly generates credit card numbers. Share to everyone for your reference. The specific analysis is as follows:
This JS code according to the credit card number generated rules randomly generated credit card number, can be verified, only for learning reference, please do not use for illegal purposes, or the consequences of conceit.
var visaprefixlist = new Array ("4539", "4556", "4916", "4532", "4929", "40240071", "4485", "4716", "4");
var mastercardprefixlist = new Array ("51", "52", "53", "54", "55");
var amexprefixlist = new Array ("34", "37");
var discoverprefixlist = new Array ("6011");
var dinersprefixlist = new Array ("300", "301", "302", "303", "36", "38");
var enrouteprefixlist = new Array ("2014", "2149");
var jcbprefixlist = new Array ("35");
var voyagerprefixlist = new Array ("8699");
function Strrev (str) {if (!str) return ';
var revstr= ';
for (i = str.length-1, i>=0; i--) Revstr+=str.charat (i) return revstr;
}/* ' prefix ' is the "start of" CC number as a string, any number of digits.
' Length ' is the length of the CC number to generate.
typically or */function completed_number (prefix, length) {var ccnumber = prefix;
Generate digits while (Ccnumber.length < (length-1)) {Ccnumber + = Math.floor (Math.random () *10); }//Reverse NuMber and convert to int var reversedccnumberstring = Strrev (Ccnumber);
var reversedccnumber = new Array (); for (Var i=0 i < Reversedccnumberstring.length i++) {Reversedccnumber[i] = parseint (Reversedccnumberstring.chara
T (i));
//calculate sum var sum = 0;
var pos = 0;
while (Pos < length-1) {odd = reversedccnumber[pos] * 2;
if (Odd > 9) {odd = 9;
sum + = odd;
if (Pos!= (length-2)) {sum = = reversedccnumber[pos +1];
POS + 2;
}//Calculate check digit var checkdigit = ((Math.floor (SUM/10) +1) *10-sum)%10;
Ccnumber + = Checkdigit;
return ccnumber;
function Credit_card_number (prefixlist, length, howmany) {var result = new Array ();
for (var i = 0; i < Howmany i++) {var randomarrayindex = Math.floor (Math.random () * prefixlist.length);
var ccnumber = prefixlist[Randomarrayindex];
Result.push (Completed_number (ccnumber, length));
return result; }
Call Method:
Save the above code as a gencc.js file
I hope this article will help you with your JavaScript programming.