SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create a random 15 letter/number piece of text?

    How do I create random 15 letter/number piece of text that is composed of letter and numbers jumbled together then automatically place that in a field? This is all of course activated by a link.
    Have a good day.

  2. #2
    SitePoint Addict
    Join Date
    Nov 2005
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use a JavaScript implementation of MD5 to create a 32-character hexadecimal number (numerals 0 - 1 and letters a - f). Then just use the first 15 characters of that for your random string:

    Code:
    var str = hex_md5(Math.random() * 100);
    str = str.substring(0,15);
    Here's an MD5 script you can use:

    http://pajhome.org.uk/crypt/md5

    To better guarantee the uniqueness of the string, try using methods of the Date object instead of Math.random().

  3. #3
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well this isnt a password just an access key to secure a low-security-threat xml file.
    Have a good day.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2005
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MD5 is used by PHP programmers to make keys for everything. It's probably the shortest route to what you want to do.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, md5 is probably an overkill here, try this
    Code:
    String.random = function(len) {
    	var s = "", t = new Date();
    	while(s.length < len)
    		s += Math.round(Math.random() * t).toString(36);
    	return s.substr(0, len);
    }
    
    // test
    alert(String.random(15))


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •