random number in a field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a simple way with FP2003 to create a form with a field such that on
loading or when someone starts to fill out the form it will generate a random
number in the field. Alternatively, I can create it automatically in the
underlying database when the form contents are used to create a record
however, I would then need the confirmation page to go retrieve this from the
server (I assume it is easier to generate it on the form itself).
THANKS
 
salmonella said:
Is there a simple way with FP2003 to create a form with a field such
that on loading or when someone starts to fill out the form it will
generate a random number in the field. Alternatively, I can create it
automatically in the underlying database when the form contents are
used to create a record however, I would then need the confirmation
page to go retrieve this from the server (I assume it is easier to
generate it on the form itself).
THANKS

You can use JS to generate a random number betwen 0 and 1
E.G.
<script type="text/javascript">
document.write(Math.random())
</script>

To obtain an integer, just perform some aritmetic
E.G.
document.write(Math.round( Math.random()*1000 ))
will generate a number between 1 and 999

But to place it in a form field, give the field an id and alter the field
value by JS:
<input type="text" id="rand" size="3" value=""/>
<script type="text/javascript">
document.getElementById("rand").value
= Math.round( Math.random()*1000 )
</script>

Or, you could be even cleverer and generate the random number on mouseover
<input type="text" id="rand" size="3" value=""
onmouseover="document.getElementById('rand').value
= Math.round( Math.random()*1000 )" />

Of course, movng the mouse out and back will generate a new number.

Another way is onload
<html>
<body onload="document.getElementById('rand').value
= Math.round( Math.random()*1000 )" >
<input type="text" id="rand" size="3" value="" />
</body>
</html>
 
many thanks!

Trevor L. said:
salmonella said:
Is there a simple way with FP2003 to create a form with a field such
that on loading or when someone starts to fill out the form it will
generate a random number in the field. Alternatively, I can create it
automatically in the underlying database when the form contents are
used to create a record however, I would then need the confirmation
page to go retrieve this from the server (I assume it is easier to
generate it on the form itself).
THANKS

You can use JS to generate a random number betwen 0 and 1
E.G.
<script type="text/javascript">
document.write(Math.random())
</script>

To obtain an integer, just perform some aritmetic
E.G.
document.write(Math.round( Math.random()*1000 ))
will generate a number between 1 and 999

But to place it in a form field, give the field an id and alter the field
value by JS:
<input type="text" id="rand" size="3" value=""/>
<script type="text/javascript">
document.getElementById("rand").value
= Math.round( Math.random()*1000 )
</script>

Or, you could be even cleverer and generate the random number on mouseover
<input type="text" id="rand" size="3" value=""
onmouseover="document.getElementById('rand').value
= Math.round( Math.random()*1000 )" />

Of course, movng the mouse out and back will generate a new number.

Another way is onload
<html>
<body onload="document.getElementById('rand').value
= Math.round( Math.random()*1000 )" >
<input type="text" id="rand" size="3" value="" />
</body>
</html>

--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
 
Back
Top