Hi
Please can someone tell me what I’ve missed from the code below. I’m new to javascript and for months now have been trying to build a form that validates. I’ve tried numerous ways of doing this and taken lots of advice from forums and websites but no matter what I do I can’t get the thing to work. I must be missing something fundamental as all my submit button seems to do is clear the form. Please please help, these months of failed attempts are driving me insane!! 😡 Thank you!!!
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“
<html>
<head>
<title>Crystaladium – NITLC students</title>
<link rel=”stylesheet” type=”text/css” href=”home.css”>
<link href=”stars.css” type=”text/css” rel=”stylesheet”>
<script type=”text/javascript”>
<!–
//the fieldcheck function(fld) checks whether each field user entry matches the expected entry, e.g. phone number
//starts with 0 and is followed by 10 numerics. If the field entries match expected entries for it returns a value of true.
//every field is checked. Use of ‘break’ prevents the code from running into the next case (field) automatically.
//The case’s are listed within the switch statement in the same order as the matching fields within the form.
function fieldcheck(fld)
{
fld.valid = false;
switch(fld.name)
{
case “firstname”:
var firstnameRegExp = /[^a-z ‘-]/i
if(firstnameRegExp.test(fld.value) == true)
{
fld.valid = true;
}
break;
case “lastname”:
var lastnameRegExp = /[^a-z ‘-]/i
if(lastnameRegExp.test(fld.value) == true)
{
fld.valid = true;
}
break;
case “email”:
var emailRegExp = /^w+([.*@w+([..
if(emailRegExp.test(fld.value) == true)
{
fld.valid = true;
}
break;
case “phone”:
var phoneRegExp = /^0d{10}$/
if(phoneRegExp.test(fld.value) == true)
{
fld.valid = true;
}
break;
case “nitlc”:
var nitlcRegExp = /^([A-Za-z]{1,2})d$/
if(nitlcRegExp.test(fld.value) == true)
{
fld.valid = true;
}
break;
case “hours”:
var hoursRegExp = /[0-24]/
if (fld.value >= 0 && fld.value <= 24)
{
fld.valid = true;
}
break;
case “address”:
var address = (/^[0-9a-zA-Zs]+$/);
if(addressRegExp.test(fld.value) == true)
{
fld.valid = true;
}
break;
case “gender_hidden”:
case “county_hidden”:
case “radio_button_hidden”:
case “generic_hidden”:
if (fld.value != 0)
{
fld.valid = true;
}
break;
}
}
// the function validate(frm)
function validate(frm)
{
// This loop sends each form user entry field for validation
for (i = 0; i <= frm.length – 2; i++)
{
fieldcheck(frm.elements[i])
}
}
// The ‘if’ statements generate an alert message where validation for any field has failed, i.e. validation returned the
//value of ‘false’ and sets the focus to that field.
if (frm.firstname.valid == false) {
window.alert(“Please enter your first name (A-Z characters only”);
firstname.focus();
}
if (frm.lastname.valid == false) {
window.alert(“Please enter your Surname (A-Z characters only”);
lastname.focus();
}
if (frm.email.valid == false) {
window.alert(“Your appear to have entered an invalid email address, please try again”);
email.focus();
}
if (frm.phone.valid == false) {
window.alert(“Please enter your 11 digit phone number including dialling code (example: 01234 789456)”);
phone.focus();
}
if (frm.nitlc.valid == false) {
window.alert(“Please enter a valid NITLC Student Number (example:DT123456789)”);
nitlc.focus();
}
if (frm.hours.valid == false) {
window.alert(“Please enter the avearage number of hours you spend on-line per day (help: number of hours between 0 and 24)”);
hours.focus();
}
if (frm.address.valid == false) {
window.alert(“Please enter your address, remember without your correct address we can’t send you your free gift”);
address.focus();
}
if (frm.gender_hidden.valid == false) {
window.alert(“Please select your gender, it will help us choose an appropriate gift for you”);
gender.focus();
}
if (frm.county_hidden.valid == false) {
window.alert(“You must select a county for your address”);
county.focus();
}
if (frm.radio_button_hidden.valid == false) {
window.alert (“Please select at least one interest.”);
radio_button.focus();
}
//–>
</script>
</head>
<body text=”#663300″>
<img alt=”Crystaladium” src=”logo.jpg”>
<div id=”menu” align=”left”>
<ul>
<li><img src=”star.gif” alt=””> <a href=
“index.html”>Home</a></li>
<li><img src=”star.gif” alt=””> <a href=”products.htm”>Our
Products</a></li>
<li><img src=”star.gif” alt=””> <a href=”buying.html”>How to Buy</a></li>
<li><img src=”star.gif” alt=””> <a href=”zodiac.htm”>Birth Stones</a></li>
<li><img src=”star.gif” alt=””> <a href=”aboutcrystals.htm”>About Crystals</a></li>
<li><img src=”star.gif” alt=””> <a href=”gifts.html”>Gift Ideas</a></li>
</ul>
</div>
<div class=”head”></div>
<div id=”header”>
<img src=”images/logo.gif” alt=””></div>
<div class=”head”></div>
<h2>Tell us about yourself and you could win a beautiful free gift…</h2>
<FORM ACTION=”” NAME=”myform” onSubmit=”validate(this);”>
<table summary=”Competition entry form”>
<tr>
<td align=”right”>First Name</td>
<td><input type=”text” name=”firstName” size=”20″ maxlength=”20″></td>
<td align=”right”>Last Name</td>
<td><input type=”text” name=”lastName” size=”20″ maxlength=”20″></td>
</tr>
<tr>
<td align=”right”>Email Address</td>
<td><input type=”text” name=”email” size=”20″ maxlength=”55″></td>
<td align=”right”>Phone</td>
<td><input type=”text” name=”phone”></td>
</tr>
<tr>
<td align=”right”>NITLC Student Number</td>
<td><input type=”text” name=”nitlc”></td>
<td align=”right”>Average hours a day spent on-line</td>
<td><input type=”text” name=”hours” size=”2″ maxlength=”2″></td>
</tr>
<tr>
<td align=”right”>Address:</td>
<td>
<textarea cols=”15″ rows=”4″ name=”address”>
</textarea></td>
<td align=”right”>Gender</td>
<td>
<select name=”Gender”>
<option value=”0″ selected=”selected”>select your gender</option>
<option value=”1″>Male</option>
<option value=”2″>Female</option>
</select>
<input TYPE=”hidden” NAME=”gender_hidden” VALUE = 0>
</td>
</tr>
<tr>
<td align=”right”>County</td>
<td><select class=”option” name=”county”>
<option value=”0″>Please Select County</option>
<option value=”1″>Bedfordshire</option>
<option value=”2″>Berkshire</option>
<option value=”3″>Buckinghamshire</option>
<option value=”4″>Cambridgeshire</option>
<option value=”5″>Cheshire</option>
<option value=”6″>Cornwall</option>
<option value=”7″>Cumberland</option>
<option value=”8″>Derbyshire</option>
<option value=”9″>Devon</option>
<option value=”10″>Dorset</option>
<option value=”11″>Durham</option>
<option value=”12″>Essex</option>
<option value=”13″>Gloucestershire</option>
<option value=”14″>Hampshire</option>
<option value=”15″>Herefordshire</option>
<option value=”16″>Huntingdonshire</option>
<option value=”17″>Kent</option>
<option value=”18″>Lancashire</option>
<option value=”19″>Leicestershire</option>
<option value=”20″>Lincolnshire</option>
<option value=”21″>London</option>
<option value=”22″>Middlesex</option>
<option value=”23″>Norfolk</option>
<option value=”24″>Northamptonshire</option>
<option value=”25″>Northumberland</option>
<option value=”26″>Nottinghamshire</option>
<option value=”27″>Oxfordshire</option>
<option value=”28″>Rutland</option>
<option value=”29″>Shropshire</option>
<option value=”30″>Somerset</option>
<option value=”31″>Staffordshire</option>
<option value=”32″>Suffolk</option>
<option value=”33″>Surrey</option>
<option value=”34″>Sussex</option>
<option value=”35″>Warwickshire</option>
<option value=”36″>Westmoorland</option>
<option value=”37″>Wiltshire</option>
<option value=”38″>Worcestershire</option>
<option value=”39″>Yorkshire</option>
</select>
<input TYPE=”hidden” NAME=”county_hidden” VALUE = 0>
</td>
<td>
<INPUT TYPE=”submit” NAME=”sb” VALUE=”Submit Form”>
<INPUT TYPE=”reset” name=”reset” value=”Clear Form”></td>
</tr>
<fieldset class=”interests”>If you win we’ll need to know what areas
interest you so we can choose a gift you’ll love.<br>
Please select from the following:<br>
<br>
<label><input type=”radio” name=”radio_button” value=”0″>Gemstones</label>
<label><input type=”radio” name=”radio_button” value=”1″>Jewellery</label>
<label><input type=”radio” name=”radio_button” value=”2″>Meditation</label>
<label><input type=”radio” name=”radio_button” value=”3″>Incense</label>
<label><input type=”radio” name=”radio_button” value=”4″>Birth Stones</label>
<label><input type=”radio” name=”radio_button” value=”5″>Tumblestones</label>
<label><input type=”radio” name=”radio_button” value=”6″>CDs</label></fieldset>
<INPUT TYPE=”hidden” NAME=”radio_button_hidden” VALUE = 0><br>
</form>
</table>
<div id=”Footer” align=”center”><br>
<a href=”aboutus.htm”>About Us</a> |
<a href=”contact.htm”>Contact Us</a> |
<a href=”sitemMap.htm”>Site Map</a> |
<a href=”disclaimer.htm”>Disclaimer</a></div>
</body>
</html>