Monday, August 11, 2014

Javascript Random String

Found a nice snippet while poking around, easy code but it's nice to have on hand for a quick copy+paste. Why code it from the bottom when you can take it from the top.


  • <script type="text/javascript"></li>
  • function randomString(i)
  • {
  • var dt = new Date();
  • var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
  • var string_length = i;
  • var randomstring = '';
  • for (var i = 0; i < string_length; i++)
  • {
  • var rnum = Math.floor(Math.random() * chars.length);
  • randomstring += chars.substring(rnum, rnum + 1)
  • }
  • return randomstring;
  • }
  • </script>
  • No comments: