I have a registration form and i want to autogenerate one of the text fields with random numbers or letters , basically when user gets to the page the field is hidden but it will populate with random numbers or text...
I have a registration form and i want to autogenerate one of the text fields with random numbers or letters , basically when user gets to the page the field is hidden but it will populate with random numbers or text...
There are millions of ways of generating random strings.
You can use the Math.random(); function:
Now assuming you have a hidden input as you mentioned:Code:function randomString(length) { var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split(''); if (! length) { length = Math.floor(Math.random() * chars.length); } var str = ''; for (var i = 0; i < length; i++) { str += chars[Math.floor(Math.random() * chars.length)]; } return str; }
Simply use DOM + our random function:HTML Code:<input id="secret" type="hidden">
Code:document.getElementById("secret").value = randomString(16);
Do you really want random? That serves no useful purpose I can think of. You might be going for unique. If so, you could use only numbers and the date object:
Code:document.getElementById("secret").value = new Date().getTime());
- John________________________
Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate
Bookmarks