JavaScript random generation of credit card number method _javascript Tips

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.