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="JavaS cript">
<!-- hide JS code
function validateForm(fo rm) // validate Survey data
{
if (!validateFirst Name(form.first Name.value)) // first name valid?
{
form.firstName. focus()
return false
}
if (!validateLastN ame(form.lastNa me.value)) // last name valid?
{
form.lastName.f ocus()
return false
}
if (!validateStude ntNumber(form.s tudentNumber.va lue)) // student number
name valid?
{
form.studentNum ber.focus()
return false
}
if (!validateHouse Name(form.house Name.value)) // house Name valid?
{
form.houseName. focus()
return false
}
if (!validateStree tName(form.stre etName.value)) // street Name valid?
{
form.streetName .focus()
return false
}
if (!validateTown( form.town.value )) // town valid?
{
form.town.focus ()
return false
}
if (!validateCount y(form.county.v alue)) // county valid?
{
form.county.foc us()
return false
}
if (!validatePostc ode(form.postco de.value)) // postcode valid?
{
form.postcode.f ocus()
return false
}
if (!validateTelep hone(form.telep hone.value)) // telephone field valid?
{
form.telephone. focus()
return false
}
if (!validateEMail (form.email.val ue)) // email valid?
{
form.email.focu s()
return false
}
if (!validateIhave (form)) // check boxes ticked?
return false
if (!validateNumHo urs(form.numHou rs.value)) // hours/day valid?
{
form.numHours.f ocus()
return false
}
alert("Congratu lations: Your data is valid!") // all data valid
return true
}
function validateFirstNa me(firstName)
{
if (isBlank(firstN ame)) // first name field blank?
{
alert("Enter your first name, please!")
return false
}
if (firstName.inde xOf(" ", 0) != -1) // two names?
{
alert("Enter one name only, please!")
return false
}
return true
}
function validateLastNam e(lastName)
{
if (isBlank(lastNa me)) // last name field blank?
{
alert("Enter your last name, please!")
return false
}
if (lastName.index Of(" ", 0) != -1) // two names?
{
alert("Enter one name only, please!")
return false
}
return true
}
function validateStudent Number(studentN umber)
{
if (isBlank(studen tNumber)) // 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.l ength; i++) // numeric?
{
var testChar = studentNumber.c harAt(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.l ength; i+++++) // numeric?
{
var testChar = studentNumber.c harAt(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 validateHouseNa me(houseName)
{
if (isBlank(houseN ame)) // house name field blank?
{
alert("Enter your house name or number, please!")
return false
}
return true
}
function validateStreetN ame(streetName)
{
if (isBlank(street Name)) // street name field blank?
{
alert("Enter your street name, please!")
return false
}
return true
}
function validateTown(to wn)
{
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 validatePostcod e(postcode)
{
if (isBlank(postco de)) // postcode field blank?
{
alert("Enter your postcode, please!")
return false
}
return true
}
function validateTelepho ne(telephone)
{
if (isBlank(teleph one)) // telephone blank?
{
alert("Enter your telephone number, please!")
return false
}
for (var i = 0; i < telephone.lengt h; i++) // telephone numeric?
{
var testChar = telephone.charA t(i)
if (testChar < "0" || testChar > "9")
{
alert("Enter a valid telephone number with no spaces, please!")
return false
}
}
if (parseInt(telep hone) < 0) // telephone <0?
{
alert("Please enter a valid telephone number")
return false
}
return true
}
function validateEMail(e mail)
{
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(f orm)
{
if (form.win95.che cked) // 1+ listen-to boxes checked?
return true
else if (form.netscape. checked)
return true
else if (form.intexplor er.checked)
return true
else if (form.winzip.ch ecked)
return true
else if (form.paintshop pro.checked)
return true
alert("Tell us what you have please")
return false
}
function validateNumHour s(numHours)
{
if (isBlank(numHou rs)) // 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(numHo urs) > 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="studentNu mber" SIZE="7"><P>
House Number/Name: <INPUT TYPE="text" NAME="houseName " SIZE="10">
Street Name: <INPUT TYPE="text" NAME="streetNam e" 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">Wi ndows95/98
<INPUT TYPE="checkbox" NAME="netscape" >Netscape
<INPUT TYPE="checkbox" NAME="intexplor er">Internet Explorer
<INPUT TYPE="checkbox" NAME="winzip">W inzip
<INPUT TYPE="checkbox" NAME="paintshop pro">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="valida teForm(this.for m)">
<INPUT TYPE="button" VALUE="Validate " onClick="valida teForm(this.for m)">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
</CENTER>
</FONT>
</BODY>
</HTML>
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="JavaS cript">
<!-- hide JS code
function validateForm(fo rm) // validate Survey data
{
if (!validateFirst Name(form.first Name.value)) // first name valid?
{
form.firstName. focus()
return false
}
if (!validateLastN ame(form.lastNa me.value)) // last name valid?
{
form.lastName.f ocus()
return false
}
if (!validateStude ntNumber(form.s tudentNumber.va lue)) // student number
name valid?
{
form.studentNum ber.focus()
return false
}
if (!validateHouse Name(form.house Name.value)) // house Name valid?
{
form.houseName. focus()
return false
}
if (!validateStree tName(form.stre etName.value)) // street Name valid?
{
form.streetName .focus()
return false
}
if (!validateTown( form.town.value )) // town valid?
{
form.town.focus ()
return false
}
if (!validateCount y(form.county.v alue)) // county valid?
{
form.county.foc us()
return false
}
if (!validatePostc ode(form.postco de.value)) // postcode valid?
{
form.postcode.f ocus()
return false
}
if (!validateTelep hone(form.telep hone.value)) // telephone field valid?
{
form.telephone. focus()
return false
}
if (!validateEMail (form.email.val ue)) // email valid?
{
form.email.focu s()
return false
}
if (!validateIhave (form)) // check boxes ticked?
return false
if (!validateNumHo urs(form.numHou rs.value)) // hours/day valid?
{
form.numHours.f ocus()
return false
}
alert("Congratu lations: Your data is valid!") // all data valid
return true
}
function validateFirstNa me(firstName)
{
if (isBlank(firstN ame)) // first name field blank?
{
alert("Enter your first name, please!")
return false
}
if (firstName.inde xOf(" ", 0) != -1) // two names?
{
alert("Enter one name only, please!")
return false
}
return true
}
function validateLastNam e(lastName)
{
if (isBlank(lastNa me)) // last name field blank?
{
alert("Enter your last name, please!")
return false
}
if (lastName.index Of(" ", 0) != -1) // two names?
{
alert("Enter one name only, please!")
return false
}
return true
}
function validateStudent Number(studentN umber)
{
if (isBlank(studen tNumber)) // 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.l ength; i++) // numeric?
{
var testChar = studentNumber.c harAt(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.l ength; i+++++) // numeric?
{
var testChar = studentNumber.c harAt(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 validateHouseNa me(houseName)
{
if (isBlank(houseN ame)) // house name field blank?
{
alert("Enter your house name or number, please!")
return false
}
return true
}
function validateStreetN ame(streetName)
{
if (isBlank(street Name)) // street name field blank?
{
alert("Enter your street name, please!")
return false
}
return true
}
function validateTown(to wn)
{
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 validatePostcod e(postcode)
{
if (isBlank(postco de)) // postcode field blank?
{
alert("Enter your postcode, please!")
return false
}
return true
}
function validateTelepho ne(telephone)
{
if (isBlank(teleph one)) // telephone blank?
{
alert("Enter your telephone number, please!")
return false
}
for (var i = 0; i < telephone.lengt h; i++) // telephone numeric?
{
var testChar = telephone.charA t(i)
if (testChar < "0" || testChar > "9")
{
alert("Enter a valid telephone number with no spaces, please!")
return false
}
}
if (parseInt(telep hone) < 0) // telephone <0?
{
alert("Please enter a valid telephone number")
return false
}
return true
}
function validateEMail(e mail)
{
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(f orm)
{
if (form.win95.che cked) // 1+ listen-to boxes checked?
return true
else if (form.netscape. checked)
return true
else if (form.intexplor er.checked)
return true
else if (form.winzip.ch ecked)
return true
else if (form.paintshop pro.checked)
return true
alert("Tell us what you have please")
return false
}
function validateNumHour s(numHours)
{
if (isBlank(numHou rs)) // 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(numHo urs) > 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="studentNu mber" SIZE="7"><P>
House Number/Name: <INPUT TYPE="text" NAME="houseName " SIZE="10">
Street Name: <INPUT TYPE="text" NAME="streetNam e" 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">Wi ndows95/98
<INPUT TYPE="checkbox" NAME="netscape" >Netscape
<INPUT TYPE="checkbox" NAME="intexplor er">Internet Explorer
<INPUT TYPE="checkbox" NAME="winzip">W inzip
<INPUT TYPE="checkbox" NAME="paintshop pro">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="valida teForm(this.for m)">
<INPUT TYPE="button" VALUE="Validate " onClick="valida teForm(this.for m)">
<INPUT TYPE="reset" VALUE="Reset">
</FORM>
</CENTER>
</FONT>
</BODY>
</HTML>
Comment