/    Sign up×
Community /Pin to ProfileBookmark

Fprm validation skipping a field

Hi
I’ve been struggling to complete a simple form validation and have now got all fields validating as required except one. The field called ‘hours’ is being skipped in the validation routine. I had it working earlier but for some reason it’s now stopped. I was playing with the reg expr on first and last name and now ‘hours’ doesnt validate any more. Please can someone tell me why this is? Page code is below, the bold sections are the relevant ones. It’s driving me crazy!!!! Thanks. ?

<!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” language=”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.

//The RegExp’s use the beginning of string character, ^, and the end of string character, $. This forces the complete string
//to match the pattern.

function fieldcheck(fld)
{

switch(fld.name)
{

case “firstName”:

var firstnameRegExp = /^[a-z]+([‘]?[a-z]+)+$/;

if(!firstnameRegExp.test(fld.value))
{

alert(“Please enter your First Name (only letters and hyphens (-) allowed)”);
return false;
}
break;

case “lastName”:

var lastnameRegExp = /^[a-z]+([‘]?[a-z]+)+$/;

if(!lastnameRegExp.test(fld.value))
{

alert(“Please enter your Surname (only letters, spaces and hyphens (-) allowed)”);
return false;
}
break;

case “email”:

var emailRegExp = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;

if(emailRegExp.test(fld.value) == false)
{

alert(“Your appear to have entered an invalid email address, please try again”);
return false;
}
break;

case “phone”:

var phoneRegExp = /^0d{4} d{6}$/

if(phoneRegExp.test(fld.value) == false)
{

alert(“Please enter your 11 digit telephone number including dialling code (example: 01234 578901)”);
return false;
}
break;

case “nitlc”:

var nitlcRegExp = /[a-z]{2}d?/

if(nitlcRegExp.test(fld.value) == false)
{

alert(“Please enter your NITLC Student Number (must include 2 letters followed by numbers, example: DT123456789”);
return false;
}
break;

[B]case “hours”:

var hours = fld.value;
if (isNaN(hours) || hours < 0 || hours > 24) {

alert(“Please enter the average number of hours you spend on-line between 0 and 24”);
return false;
}

break;[/B]

case “address”:

var addressRegExp = /^[0-9a-zA-Zs]+$/;
if(addressRegExp.test(fld.value) == false)
{

alert(“Please enter your address, we’ll need it to send your free gift if you win!”);
return false;
}

}
return true;
}

// the function validate(frm)
function validate(frm)
{
for (i = 0; i <= frm.length – 1; i++)
{
if (!fieldcheck(frm.elements[i]))
{
frm.elements[i].focus();
frm.elements[i].select();
return false;
}
}
alert(“Form is valid!”)

}

//–>

</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” onSubmit=”return validate(this);” action=”mailto:[email protected]” method=”POST” enctype=”text/plain”>

<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>
[B]<td align=”right”>Average hours a day spent on-line</td>
<td><input type=”text” name=”hours” size=”2″ maxlength=”2″></td>[/B]

</tr>
<tr>
<td align=”right”>Address:</td>
<td>
<textarea cols=”15″ rows=”4″ name=”address”>
</textarea></td>
<td>
<INPUT TYPE=”button” NAME=”sb” VALUE=”Submit Form” onClick=”validate(this.form);”>
<INPUT TYPE=”reset” name=”reset” value=”Clear Form”></td>
</tr>
</table>
</form>
<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_IsfineJul 08.2009 — Empty field == null == 0 != NAN

Try:
[CODE]if (isNaN(hours) || !/S/.test( hours ) || hours < 0 || hours > 24)
...[/CODE]
Copy linkTweet thisAlerts:
@A_noviceauthorJul 08.2009 — Many thanks, much appreciated.?
Copy linkTweet thisAlerts:
@peachskittleJul 08.2009 — Empty field == null == 0 != NAN

Try:
[CODE]if (isNaN(hours) || !/S/.test( hours ) || hours < 0 || hours > 24)
...[/CODE]
[/QUOTE]



Was just passing through, looking around to try and learn some stuff....

Can you help explain the second or for me? I'm curious what it does...
Copy linkTweet thisAlerts:
@Sterling_IsfineJul 08.2009 — Was just passing through, looking around to try and learn some stuff....

Can you help explain the second or for me? I'm curious what it does...[/QUOTE]

"A non-whitespace character was not found".

Then the acceptance of "" or " " by isNaN() isn't a problem.
×

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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...