Generate random 6-digit alpha-numeric code.

Posted on 2007-10-20
Last Modified: 2012-06-27
I need a javascript that will be envoked via an onclick

I need a random 6-Digit alpha-numeric (uppercase) id created when the user clicks the link provided. The id would then populate a form field.

So basically the user click the link and the form field gets populated with the 6-digit id.

Question by:rcmb
    LVL 8

    Accepted Solution

    LVL 82

    Expert Comment

    add upper case characters if you want them as well
    <script type="text/javascript"><!--
    function getID(idLength)
          var chars="0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
          var min=0;
          var max=chars.length-1;
          var id="";
          for(var i=0; i<idLength;i++)
                id+=chars[ Math.floor(Math.random()*(max - min + 1) + min) ];
    return id;
    LVL 12

    Author Comment

    Both worked equally as well but MrRobot was first. Thanks for your help and quick turnaround.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Adobe Dreamweaver CC 2015

    Adobe Dreamweaver Creative Cloud is used by web designers and front-end developers and allows you to visualize your site in real-time as you code. This course covers exam objectives for the Adobe Certified Associate (ACA) certification.

    Suggested Solutions

    Title # Comments Views Activity
    PHP Radio Array 4 27
    jquery 3 and protoype 9 23
    Please explain a jQuery promise 5 20
    Is there a way to optimise this code? 3 16
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    In Part 1 ( we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now