Hi,
Im trying to validate a form, all the validating works apart from one field.
This particular field must consist of the first 2 characters as letters, &
the following 5 as numbers. And if it dosent meet these requirments an error
message will be displayed. I have pasted the code (and highlighted the
relevant parts) below in the hope that someone can help me out with this.
Ive been trying to suss it out all week & it's driving me nuts!
Many Thanks
Mark
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- hide JS code
function validateForm(form) // validate Survey data
{
if (!validateFirstName(form.firstName.value)) // first name valid?
{
form.firstName.focus()
return false
}
if (!validateLastName(form.lastName.value)) // last name valid?
{
form.lastName.focus()
return false
}
if (!validateStudentNumber(form.studentNumber.value)) // student number
name valid?
{
form.studentNumber.focus()
return false
}
if (!validateHouseName(form.houseName.value)) // house Name valid?
{
form.houseName.focus()
return false
}
if (!validateStreetName(form.streetName.value)) // street Name valid?
{
form.streetName.focus()
return false
}
if (!validateTown(form.town.value)) // town valid?
{
form.town.focus()
return false
}
if (!validateCounty(form.county.value)) // county valid?
{
form.county.focus()
return false
}
if (!validatePostcode(form.postcode.value)) // postcode valid?
{
form.postcode.focus()
return false
}
if (!validateTelephone(form.telephone.value)) // telephone field valid?
{
form.telephone.focus()
return false
}
if (!validateEMail(form.email.value)) // email valid?
{
form.email.focus()
return false
}
if (!validateIhave(form)) // check boxes ticked?
return false
if (!validateNumHours(form.numHours.value)) // hours/day valid?
{
form.numHours.focus()
return false
}
alert("Congratulations: Your data is valid!") // all data valid
return true
}
function validateFirstName(firstName)
{
if (isBlank(firstName)) // first name field blank?
{
alert("Enter your first name, please!")
return false
}
if (firstName.indexOf(" ", 0) != -1) // two names?
{
alert("Enter one name only, please!")
return false
}
return true
}
function validateLastName(lastName)
{
if (isBlank(lastName)) // last name field blank?
{
alert("Enter your last name, please!")
return false
}
if (lastName.indexOf(" ", 0) != -1) // two names?
{
alert("Enter one name only, please!")
return false
}
return true
}
function validateStudentNumber(studentNumber)
{
if (isBlank(studentNumber)) // blank?
{
alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
for (var i = 0; i < studentNumber.length; i++) // numeric?
{
var testChar = studentNumber.charAt(i)
if (testChar < "A" || testChar > "Z")
{
alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
for (var i = 0; i < studentNumber.length; i+++++) // numeric?
{
var testChar = studentNumber.charAt(i)
if (testChar < "0" || testChar > "9")
}
alert("Enter your student number please in the format of 2 uppercase
letters followed by 5 numbers")
return false
}
return true
}
function validateHouseName(houseName)
{
if (isBlank(houseName)) // house name field blank?
{
alert("Enter your house name or number, please!")
return false
}
return true
}
function validateStreetName(streetName)
{
if (isBlank(streetName)) // street name field blank?
{
alert("Enter your street name, please!")
return false
}
return true
}
function validateTown(town)
{
if (isBlank(town)) // town field blank?
{
alert("Enter your town, please!")
return false
}
return true
}
function validateCounty(county)
{
if (isBlank(county)) // county field blank?
{
alert("Enter your county, please!")
return false
}
return true
}
function validatePostcode(postcode)
{
if (isBlank(postcode)) // postcode field blank?
{
alert("Enter your postcode, please!")
return false
}
return true
}
function validateTelephone(telephone)
{
if (isBlank(telephone)) // telephone blank?
{
alert("Enter your telephone number, please!")
return false
}
for (var i = 0; i < telephone.length; i++) // telephone numeric?
{
var testChar = telephone.charAt(i)
if (testChar < "0" || testChar > "9")
{
alert("Enter a valid telephone number with no spaces, please!")
return false
}
}
if (parseInt(telephone) < 0) // telephone <0?
{
alert("Please enter a valid telephone number")
return false
}
return true
}
function validateEMail(email)
{
if (isBlank(email)) // email blank?
{
alert("Enter your email address, please!")
return false
}
var atsignPos = email.indexOf("@", 0) // check for @
if (atsignPos == -1)
{
alert("Enter a valid email address with an @, please!")
return false
}
if (email.indexOf(".", atsignPos) == -1) // check for . after @
{
alert("Enter a valid email domain after the @, please!")
return false
}
return true
}
function validateIhave(form)
{
if (form.win95.checked) // 1+ listen-to boxes checked?
return true
else if (form.netscape.checked)
return true
else if (form.intexplorer.checked)
return true
else if (form.winzip.checked)
return true
else if (form.paintshoppro.checked)
return true
alert("Tell us what you have please")
return false
}
function validateNumHours(numHours)
{
if (isBlank(numHours)) // numHours blank?
{
alert("Enter the number of hours a day you enjoy music, please!")
return false
}
for (var i = 0; i < numHours.length; i++) // numHours numeric?
{
var testChar = numHours.charAt(i)
if (testChar < "0" || testChar > "9")
{
alert("Enter a non-negative integer number of hours, please!")
return false
}
}
if (parseInt(numHours) > 24) // numHours > 24?
{
alert("Sorry, only 24 hours in a day. Try again, please!")
return false
}
return true
}
function isBlank(testStr)
{
if (testStr.length == 0) // nothing entered?
return true
for (var i = 0; i <= testStr.length-1; i++) // all spaces?
if (testStr.charAt(i) != " ")
return false
return true
}
// end JS hide -->
</SCRIPT>
<BODY BGCOLOR="white" TEXT =#0099FF> <FONT FACE="ARIAL">
<CENTER>
<H1 class="style1">Javascript Assessment</H1><BR>
<FORM NAME="assesForm">
<B>First Name: <INPUT TYPE="text" NAME="firstName" SIZE="12">
Last Name: <INPUT TYPE="text" NAME="lastName" SIZE="12">
Student Number: <INPUT TYPE="text" NAME="studentNumber" SIZE="7"><P>
House Number/Name: <INPUT TYPE="text" NAME="houseName" SIZE="10">
Street Name: <INPUT TYPE="text" NAME="streetName" SIZE="15"><P>
Town: <INPUT TYPE="text" NAME="town" SIZE="15">
County: <INPUT TYPE="text" NAME="county" SIZE="15">
Postcode: <INPUT TYPE="text" NAME="postcode" SIZE="8"><P class="style1">
Telephone: <INPUT TYPE="text" NAME="telephone" SIZE="12">
EMail: <INPUT TYPE="text" NAME="email" SIZE="26"><P class="style1">
I have: <INPUT TYPE="checkbox" NAME="win95">Windows95/98
<INPUT TYPE="checkbox" NAME="netscape">Netscape
<INPUT TYPE="checkbox" NAME="intexplorer">Internet Explorer
<INPUT TYPE="checkbox" NAME="winzip">Winzip
<INPUT TYPE="checkbox" NAME="paintshoppro">Paintshop Pro<P
class="style1">
I spend approximately <INPUT TYPE="text" NAME="numHours" SIZE="2"
MAXLENGTH="2"> hours a day connected to the internet.<P class="style1">
<INPUT TYPE="submit" VALUE="Submit" onClick="validateForm(this.form)">
<INPUT TYPE="button" VALUE="Validate" onClick="validateForm(this.form)">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
</CENTER>
</FONT>
</BODY>
</HTML>