FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Javascript random code generator script needed please





DoctorBeaver
Could anyone write me a simple Javascript routine to generate an n character alpha-numeric code? (& show what goes in the head and what in the body) I want to be able to alter the length of the code generated by changing a variable's value rather than by input.

I've got a Javascript form on the page that sends an email and I want to add a unique code to it.

All I need is a function that returns the code to a variable which I can then display in the form (displaying it in the form I can already do - I think, although I've not actually tried it yet).

I've looked through various free applet sites but all the ones I've found require a seed to be input at run time and that's no use to me. I've also looked through tutorial sites but they only show how to generate a random number, not an alphanumeric code.

Thanks.
LukeakaDanish
Code:


chars = new Array ('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e'...#fill these out yourself :D#);

charCount = chars.length();

stringLength = 5;
var outputString;

function mkRandomString() {
   i = 0;
   do {
      random = Math.floor(Math.random());
      random *= charCount;
      random = chars[random];
      outputString += parseString(random);
      i++;
   }
   while (i<stringLength);
}



Thats should do it - the string is outputted to the variable "outputString"

Hope it works for you
DoctorBeaver
Thanks a lot. Do I need to add something like:-

return outputString

at the end of the function?
venc
Am I supposed to understand all this?
LukeakaDanish
DoctorBeaver wrote:
Thanks a lot. Do I need to add something like:-

return outputString

at the end of the function?


If you call the function like this:

Code:

something = mkRandomString();


Then yes, otherwise just do the following when you want to call the function:

Code:

mkRandomString();


The outputString will have been set and you can use it freely in your other function Very Happy

@venc: I dunno - but your message looks like spam to me Mad
DoctorBeaver
OK, maybe I don't know how to get the value into the form.

The code for that field is

Code:

<input type="text" name="random_code" value="this is where I want the random code to be displayed" size="10" maxlength="10" tabindex="1" style="position:absolute;z-index:2;left:97px;top:60px;width:170px;height:22px;font-size:10pt;font-family:Arial;font-weight:normal;font-style:normal;color:#000000;background-color:#FFFFFF;">


I need to get the random code into the value= bit.
LukeakaDanish
Your welcome - pleased to help! Wink
Nyizsa
Something like
Code:
Document.getElementById('random_code').value=outputString;

I'm not sure about the syntax - you may figure that out yourself. Cool
Related topics
Random page generator
How to make a Random code in PHP !
[c++]random number generator doesnt work
100F$ for Javascript Menu code(done. LOCK please)
AJAX tutorial [2nd part now updated]
Problem connecting to MySQL database with PHP [RESOLVED]
my PHP code benchmark script that got some problems
Need Help with this html code
JAVA - random name generator
Using Javascript to Encrypt Data then POST to PHP
JavaScript Games
Microsoft confirms that XP contains random number generator
JS Random Include txt-File
Noobie's code
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.