Question : How do i verify an age element using javascript?

I have to create errors using javascript on elements in a Log In Form. I have to create an error if a element is empty, if the email doesn't contain @ and if the age is numeric and =>18! I have mastered the empty cell and email errors but age is getting me! I am no computer expert!!!!! Javascript is beyond me.
Attachments:
 
FlexIt Login form
 

Answer : How do i verify an age element using javascript?

test page :

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:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
<!-- saved from url=(0022)http://internet.e-mail -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html lang="en-au">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Script-Type" content="text/javascript" />
    <style type="text/css" media="screen">
				@import url(../styles/ips2.css);
		</style>
<style type="text/css" media="screen">
				@import url(../styles/unit2.css);
		</style>
<link href="../styles/ips2_print.css" media="print" type="text/css" rel="stylesheet" />


<title>Login form</title>
<script type="text/javascript" language="javascript">

	function validate()
	{
		var allTextboxesAreNotEmpty = true;
		var textboxes = document.forms["form1"].getElementsByTagName("input");
		for(var i=0;i<textboxes.length;i++) {
			if(textboxes[i].value.length == 0) {
			allTextboxesAreNotEmpty = false;
			break;
			}
		}

		var emailIsValid = checkEmail( document.getElementById("email").value ); 
		
		var isAdult = adulthood(  document.getElementById("age").value );

		var stateIsSelected = (document.getElementById("state").selectedIndex > 0)
		
		return allTextboxesAreNotEmpty && emailIsValid && isAdult && stateIsSelected;	
	}
	
	
	function checkEmail(email) 
	{
		return /^([a-zA-Z0-9_.-])[email protected](([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(email);
	}
	
	function adulthood(age)
	{
		return parseInt(age) >= 18;
	}

</script>
</head>		

  <body>
  <noscript>This page is better viewed with JavaScript enabled</noscript>
<a name="a" id="a" class="tocMarker"></a>

<div class="lyrTop" id="lyrTop">
<span class="topNav">
			| <a href="#" class="topNav" onClick="printPage();" onKeyPress="return handleEnter(ACTION_PRINT,event);">Print</a>
			| <a href="#" class="topNav" onClick="window.close(); return false;" onKeyPress="return handleEnter(ACTION_CLOSE,event);">Close</a> |
</span>
<noscript><span>[Javascript disabled - To close this window, press Ctrl + W (PC), or APPLE + W (Mac)]</span></noscript>
</div>
	<form name="form1" action="#" method="get" onSubmit="return validate();">
	   <div style=" margin-left:100;background-color:#F9FAE7;width:300">
	   <table>
	   <tr><td colspan="2"><h1>Flex-IT Registration</h1></td></tr>
	   <tr><td></td></tr>
	   <tr><td > First name:</td><td><input type="text" id="firstname" name="firstname" /></td></tr>
	    <tr><td>Last name: </td><td><input type="text" id="lastname" name="lastname" /></td></tr>
	    <tr><td>Email: </td><td><input id="email" name="email" size="35"  /></td></tr>
	    <tr><td>Age: </td><td><input id="age" name="age" size="3" maxlength="2"/></td></tr>
	    <tr><td>State</td><td><select id="state" name="state">
	       <option value="" selected="selected">Choose a state...</option>
	       <option value="NSW">NSW</option>
	       <option value="QLD">QLD</option>
	       <option value="VIC">VIC</option>
	       <option value="SA">SA</option>
	       <option value="WA">WA</option>
	       <option value="TAS">TAS</option>
	       <option value="NT">NT</option>
	       </select></td></tr>
	       </table>
	     <tr><td><input type="button" value="Submit" onClick="validate()"/></td></tr>  
	   </div> 
	</form>
  </body>
</html>
Random Solutions  
 
programming4us programming4us