JavaScript to create a random string method with numeric letters summary _javascript Tips

Source: Internet
Author: User

First type:

The code from http://caibaojian.com/js-random-string.html
function Makeid ()
{
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (Var i=0 i < 5; i++)
    text + possible.charat (Math.floor (Math.random () * possible.length));

  return text;

Second: Do not need to enter the character set together

function RandomString (L) {
  var s= ';
  var randomchar=function () {
   var n= math.floor (Math.random () *62);
   if (n<10) return n; 1-10
   if (n<36) return String.fromCharCode (n+55),//a-z return
   string.fromcharcode (n+61);//a-z
  } While
  (s.length< L) s+= Randomchar ();
  return s;
}

alert(randomstring(5))

Third: Supports custom character length and feature character set combination

function RandomString (len, charSet) {
  CharSet = CharSet | | ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ';
  var randomstring = ';
  for (var i = 0; i < len; i++) {
   var randompoz = Math.floor (Math.random () * charset.length);
   RandomString + = charset.substring (randompoz,randompoz+1);
  }
  return randomstring;
}

Call with default CharSet [a-za-z0-9] or send in your own:

var randomvalue = RandomString (5);

var randomvalue = randomstring (5, ' pickcharsfromthisset ');

Demo screenshot

The above is JavaScript to create a random string with digital letters three methods summary, there is a need to reference learning.

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.