Javascript Question

This is a discussion on "Javascript Question" within the JavaScript Forum section. This forum, and the thread "Javascript Question are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Jul 17th, 2007, 10:04
New Member
Join Date: Jul 2007
Location: Scotland
Age: 21
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript Question

I have a basic webpage that has an email form on it. Normally I would use captcha to secure this webform but unfortunatly the type of server that the website is stored on does not support this (for some reason) so this is out of the question.

I have decided to create my own basic form of captcha. I was going to create a script that would generate three random numbers and display them and the user would simply enter these random numbers to show that they are human. After a bit of research i found that the code i needed to generate the random numbers is:

Code: Select all
var randomnumber=Math.floor(Math.random()*51)
The above line would generate a random number between 1 and 50. I would do this three times and generate three random numbers.

My question is, where do i enter this code? Is it in an external javascript file? and how would i go about displaying the contents of randomnumber on my webpage?

Here is the code for my form, as you can see its VERY basic
Code: Select all
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Visitor Reply</title>
<style type="text/css">
<!--
.style1 {color: #FF0000}
-->
</style>
</head>
<body>
<td width="50%"><table width="100%" border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td><div>
<div id="TXTOBJ7D63114E2E92421">
<div link="#ff0000">
<pre class="style1">Please complete the form below to request a tee time. We shall contact you by e.mail as soon as we can. </pre>
</div>
</div>
<div id="TXTOBJ7D733713C25531">
<div></div>
</div>
</div>
<p>&nbsp;</p> 
<p>&nbsp;</p> </td> 
</tr> 
<tr> 
<td> 
 
<form name="contactForm" action="process.php" method="POST" /n> 
<input type="hidden" name="recipient" value="email@url" /> 
<input type="hidden" name="required" value="name" /> 
 
<input type="hidden" name="missing_fields_redirect" value="../ferror.htm" /> 
<table width=85%> 
<tr> 
<td> <pre class="style1">Name:</pre></td> 
<td align="right"><div align="left"> 
<input name="name" type="text" class="body" id="name" style="width:160px;" size=40 maxlength=40> 
</div></td> 
</tr> 
<tr> 
<td> <pre class="style1">Club:</pre></td> 
<td align="right"><div align="left"> 
<input name="club" type="text" class="body" id="club" style="width:160px;" size=20 maxlength=80> 
</div></td> 
</tr> 
<tr> 
<td> <pre class="style1">Email:</pre></td> 
<td align="right"><div align="left"> 
<input name="email" type="text" class="body" id="email" style="width:160px;" size=20 maxlength=80> 
</div></td> 
</tr> 
<tr>
<td height="26"> <pre class="style1">Number booking: </pre></td>
<td align="right" class="textfooter"><div align="left">
<input name="number" type="text" class="body" id="number" style="width:160px;" size="20" maxlength="40" />
</div></td>
</tr>
<tr> 
<td height="26"><pre class="style1">Date Required: </pre></td> 
<td align="right" class="textfooter"><div align="left">
<input name="date" type="text" class="body" id="date" style="width:160px;" size="20" maxlength="40" />
</div></td> 
</tr> 
<tr>
<td><pre class="style1">Preffered Time: </pre></td>
<td align="right"><div align="left">
<input name="time" type="text" class="body" id="time" style="width:160px;" size="20" maxlength="40" />
</div></td>
</tr>
<tr> 
<td> <pre class="style1">Comments:</pre></td> 
<td align="right"><div align="left"> 
<textarea name="comments" cols="25" rows="4" class="body" id="comments"></textarea> 
</div></td> 
</tr> 
<td align="center">&nbsp;</td>
<td align="center"> <div align="left">
<div align="left"><br> 
</div></td>
<td width="1%" align="center">&nbsp;</td>
<tr> 
<td height="62" colspan="2" align="right"><div align="left" class="copy style1">
<table width="841" border="0">
<tr>
<td width="361" height="29">&nbsp;
 
</td> 
<td width="468">&nbsp;</td>
</tr>
<tr>
<td height="29">&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
<pre>&nbsp;</pre>
</div></td>
</tr> 
</table> 
<tr> 
<td colspan="2" align="right"><div align="left">
<input name="submit" type="submit" class="Button" value="Submit" />
<input type="reset" value=" Clear" class="Button"> 
</div></td> 
</tr> 
</table> 
</form></td> 
</tr> 
</table>
<p>&nbsp;</p>
<p>&nbsp;</p></td> 
</tr> 
</table> 
</body>
</html>
Thanks

Last edited by karinne; Jul 18th, 2007 at 11:49. Reason: Please use [code]...[/code] tags when displaying code!
Reply With Quote

  #2 (permalink)  
Old Jul 22nd, 2007, 01:42
Junior Member
Join Date: Jan 2007
Location: Michigan
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Javascript Question

first of all
although the code u found to generate a random number works fine if you only have to produce one random number, it will have problems when ur trying to produce, say, three random numbers. you'll see as i descuss. the best thing to do would be to put the random number code into a value returning function so your code should look like this:
Code: Select all
function randomNumber(){
  return Math.floor(Math.random()*51);
}
the reason to do this is because if u store it into a variable, the browser will only find one random number and store it into the variable "randomnumber" once and the number becomes fixed. the problem with that is that you get the same number any time u call the "randomnumber" variable. if u put it in a function like the code i gave you, the browser will wait until the function is called to exicure the random numer generating code which means the every time u call "randomnumber()" as a function, the code will be re-executed creating a new number each time. which is what you want.

as to were to put the code, anywere between and ,script. tags is acceptable, but the normal thing is between those tags inside the head tag. like this:
Code: Select all
<html>
<head>
..other stuff in head tag...
<script language="javascript">
function randomNumber(){
  return Math.floor(Math.random()*51);
}
</script>
</head>
...and so on
whenever you want to display another random number, just call it inside a document.write() method to output it.

put this code wherever on your page you want a random number:
Code: Select all
<script language="javascript">document.write(randomnumber());</script>
Reply With Quote
Reply

Tags
randomnumber

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript dharvesh JavaScript Forum 4 Oct 21st, 2007 01:28
New to JavaScript Syd JavaScript Forum 3 Nov 7th, 2006 00:53
Help with Javascript in php Martin McPherson PHP Forum 3 Oct 27th, 2006 11:47
Javascript problem/question kb3llm129 JavaScript Forum 0 May 6th, 2006 22:03
Hello and question: Javascript, Perl or PHP? masonbarge Introduce Yourself 9 Mar 1st, 2006 08:21


All times are GMT. The time now is 04:47.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43