SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Getting a random ID put on a page

  1. #1
    SitePoint Evangelist laflair13's Avatar
    Join Date
    Nov 2004
    Location
    TN
    Posts
    577
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Getting a random ID put on a page

    Hey all,

    I am wanting something to generate a unique 5 digit ID, like Ah45P or something along those line.

    Heres my set up, someone comes to the site, fills out a form, they go to a thank you page, then they see "click to get your Unique ID", upon click they go to another page that says.. "Your Unique ID is 12345" . (hope that made sense)

    Is there any tutorials that I can do that with?
    Thanks In Advance

    David

  2. #2
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,422
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    Hi there,

    This will do what you want:

    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Generate unique ID</title>
      </head>
      
      <body>
        <p>Your unique ID is: <span id="uniqueID"></span></p>
        
        <script>
          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;
          }
          
          document.getElementById("uniqueID").innerHTML = makeid();
        </script>
      </body>
    </html>
    I'm afraid I can't take credit for it though, it's lifted straight from Stack Overflow: http://stackoverflow.com/questions/1...-in-javascript
    Where there is a good discussion of the best way to do this.

    Hope that helps.
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  3. #3
    SitePoint Evangelist laflair13's Avatar
    Join Date
    Nov 2004
    Location
    TN
    Posts
    577
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked perfect.

    Thank You
    Thanks In Advance

    David

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
  •