Link to home
Start Free TrialLog in
Avatar of rcmb
rcmb

asked on

Generate random 6-digit alpha-numeric code.

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.

Thanks,
Curtis
ASKER CERTIFIED SOLUTION
Avatar of MrRobot
MrRobot
Flag of Türkiye image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hielo
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";
      chars=chars.split(",");
      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;
}
alert(getID(6));
//--></script>
Avatar of rcmb
rcmb

ASKER

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

Curtis