View Full Version : Create a random number in the value= attribute on a hidden input field

08-14-2006, 03:09 AM
I'm wanting to have a randomly generated number as the value of a tag each time I reload the page.

<form action="whatever.asp" method="post" name="myform" onsubmit="return ValidateForm(this)">

<input type="hidden" name="v46" value="8773">


I want to replace the 8773 of the attribute value="8873 with a randomly generated number between 1-9999. So far I have found the code below which generates a random number as the value in a visible input field.

What I need it to do is change in the actual code of the page each time the page reloads for a hidden input value. So evertime you refresh the page the value="8873" in the actual code changes to a different number.

The JavaScript I have so far is

<script type="text/javascript">
var n=9999;
var rand = Math.ceil(Math.random()*n);

Any Ideas? Any help would be greatly appreciated.

08-14-2006, 04:33 AM
The code you have will change the value as you ask, providing that element is in the first form of the page. There's only one thing that puzzles me:
change in the actual code of the page
This is not possible without server-side scripting, but more pertinently... why?

08-14-2006, 05:40 AM
The code I am using is on a search page, each page that loads needs to have a different value="" or if two people used the same page at the same time it could cause a mix up in the results returned.

Would it make any difference between using a hidden vs a non hidden input field?

08-14-2006, 05:52 AM

The best way to do this is still server-side. Server-side you don't need to generate a random number, since you can use numbers incrementally. This also removes the risk of user tampering.