Generate a random string of any length

Description Javascript

This function generates a random string of x many characters.

The Code Download

  1. function rndString(slength) {
  2.     var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz";
  3.     if (!slength || typeof(slength) != 'number') {
  4.         var slength = 8;
  5.     }
  6.     var rs = '';
  7.     for (var i=0;i<slength;i++) {
  8.         var rnum = Math.floor(Math.random() * chars.length);
  9.         rs += chars.substring(rnum,rnum+1);
  10.     }
  11.     return rs;
  12. }

Credits

Added by JC on 13th November 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment