Submit Your Article Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register
 

Go Back   WebForumz.com > The Code > JavaScript

Reply
 
LinkBack Thread Tools
Old July 17th, 2007, 06:04 AM   #1
New Member
 

Join Date: Jul 2007
Location: Scotland
Age: 23
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 russb is on a distinguished road
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:
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:
<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; July 18th, 2007 at 07:49 AM.. Reason: Please use [code]...[/code] tags when displaying code!
russb is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old July 21st, 2007, 09:42 PM   #2
New Member
 

Join Date: Jan 2007
Location: Michigan
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Altering Power: 0 joe3dge is on a distinguished road
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:
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:
<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:
<script language="javascript">document.write(randomnumber());</script>
joe3dge is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Bookmarks

Tags
randomnumber


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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 4 October 20th, 2007 09:28 PM
New to JavaScript Syd JavaScript 3 November 6th, 2006 08:53 PM
Help with Javascript in php Martin McPherson PHP 3 October 27th, 2006 07:47 AM
Javascript problem/question kb3llm129 JavaScript 0 May 6th, 2006 06:03 PM


Search Engine Optimization by vBSEO 3.2.0 RC8