/    Sign up×
Community /Pin to ProfileBookmark

cant get form validation working

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”
http://www.w3.org/TR/html4/strict.dtd“>

<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+)*@w+([.-]?w+)*(.w{2,3})+$/;
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=””>&nbsp;<a href=
“index.html”>Home</a></li>
<li><img src=”star.gif” alt=””>&nbsp;<a href=”products.htm”>Our
Products</a></li>

<li><img src=”star.gif” alt=””>&nbsp;<a href=”buying.html”>How to Buy</a></li>
<li><img src=”star.gif” alt=””>&nbsp;<a href=”zodiac.htm”>Birth Stones</a></li>
<li><img src=”star.gif” alt=””>&nbsp;<a href=”aboutcrystals.htm”>About Crystals</a></li>
<li><img src=”star.gif” alt=””>&nbsp;<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>

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineJun 12.2009 — taken lots of advice from forums and websites[/quote]
Here's the missing advice:

FireFox > Tools > Error Console

Press 'Clear', press 'Errors'.

Reload page.

Look at errors shown in console.
Copy linkTweet thisAlerts:
@A_noviceauthorJun 12.2009 — Hi

Thanks for your reply. The error in error console states that frm is not defined, it's relating to the following when I click on it. Unfortunately the errors doesnt help me as I dont know what it means so dont know how to fix it.

if (frm.firstname.valid == false) {

I have a feeling that there's something missing from my script to tell it to validate, I have lots of if statements but nothing else. I've also done other versions of this form that had no errors and it still didnt work, am too new to scripting to be able to figure out why.

Any help you can give would be much appreciated.
Copy linkTweet thisAlerts:
@Sterling_IsfineJun 12.2009 — Hi

Thanks for your reply. The error in error console states that frm is not defined, it's relating to the following when I click on it.[/QUOTE]
No - that error appears before you click anything, which in itself is a clue to the problem.

You need to look closely at the structure of the [I]validate[/I] function, which is a lot shorter than you may think.
Copy linkTweet thisAlerts:
@bcmannJun 12.2009 — I took a look at your code, I would work things a little different (see code). I don't know how to make things work the way you were approaching it. These code isn't complete, but hopefully it will help point you in the right direction.

What are you planning on doing with the data from the form? Your code didn't specify an action, so when the user fills out the form, it's just going to clear the form and do nothing. I usually put the fields into a database or at least an email using PHP.

Anyway... hope this helps.

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<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">
function validate_form(){
var frm = document.getElementById("myform");

for(var i=0; i<frm.elements.length; i++){
//alert(frm.elements[i].name);
switch(frm.elements[i].name){

case "firstName":
var firstnameRegExp = /[^a-z '-]/i
if(firstnameRegExp.test(frm.elements[i].value) == true){
alert("Please enter your first name (A-Z characters only");
firstname.focus();
return (false);
}
break;

case "lastname":
var lastnameRegExp = /[^a-z '-]/i
if(!lastnameRegExp.test(frm.elements[i].value)){
window.alert("Please enter your Surname (A-Z characters only");
lastname.focus();
}
break;

case "email":
var emailRegExp = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
if(!emailRegExp.test(frm.elements[i].value)){
window.alert("Your appear to have entered an invalid email address, please try again");
email.focus();
}
break;

case "phone":
var phoneRegExp = /^0d{10}$/
if(!phoneRegExp.test(frm.elements[i].value)){
window.alert("Please enter your 11 digit phone number including dialling code (example: 01234 789456)");
phone.focus();
}
break;

case "nitlc":
var nitlcRegExp = /^([A-Za-z]{1,2})d$/
if(!nitlcRegExp.test(frm.elements[i].value)){
window.alert("Please enter a valid NITLC Student Number (exampleT123456789)");
nitlc.focus();
}
break;

case "hours":
var hoursRegExp = /[0-24]/
if (frm.elements[i].value < 0 && frm.elements[i].value > 24){
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();
}
break;

case "address":
var addressRegExp = (/^[0-9a-zA-Zs]+$/);
if(!addressRegExp.test(frm.elements[i].value)){
window.alert("Please enter your address, remember without your correct address we can't send you your free gift");
address.focus();
}
break;

}
}
}

//-->

</script>
</head>
<body text="#663300">
<img alt="Crystaladium" src="logo.jpg">
<div id="menu" align="left">
<ul>
<li><img src="star.gif" alt="">&nbsp;<a href=
"index.html">Home</a></li>
<li><img src="star.gif" alt="">&nbsp;<a href="products.htm">Our
Products</a></li>

<li><img src="star.gif" alt="">&nbsp;<a href="buying.html">How to Buy</a></li>
<li><img src="star.gif" alt="">&nbsp;<a href="zodiac.htm">Birth Stones</a></li>
<li><img src="star.gif" alt="">&nbsp;<a href="aboutcrystals.htm">About Crystals</a></li>
<li><img src="star.gif" alt="">&nbsp;<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 NAME="myform" id="myform" onSubmit="return validate_form();" method="post" >
<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>
[/CODE]
×

Success!

Help @A_novice spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...