468,321 Members | 1,814 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,321 developers. It's quick & easy.

Validating Text box with multiple variables

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>
Jul 20 '05 #1
3 13621
Lee
Mark said:
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
}


Your function first checks to see if the field is blank,
then checks to make sure that *every* character is in [A-Z],
then it checks to make sure that *every* character is in [0-9]
(although I'm not sure what "i+++++" is supposed to mean).

Compare what you're actually doing to what your instructor
said to do, and you should be able to figure it out.

Jul 20 '05 #2
Mark wrote:
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
}


Some people will not have any of these programs. If you want to make
sure that people select /something/, add an option for "None of the above".

Jul 20 '05 #3
JRS: In article <Nq***************@newsfep3-gui.server.ntli.net>, seen
in news:comp.lang.javascript, Mark <no*@chance.com> posted at Fri, 7 Nov
2003 17:15:30 :-
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!
There is no need to post the lot; you can reduce it to a test case by
removing all other elements, and their code, from the Form.

In fact, as you know that the fault is in validateStudentNumber, there
was no need to post more than just that function and a description of
what it should input and output.

this is an international newsgroup; use of slang is discourteous - it
may set foreigners to searching dictionaries, fruitlessly.
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
}

Zerothly, since form is a well-known word in relation to forms, it would
IMHO be nicer not to use it as an identifier.

Firstly, ISTM that validateForm can be much simplified. since the IF
statements are all similar. Consider

function validateForm(Frm) {
with (Frm) return (
OKF(validateFirstName, firstName) &&
OKF(validateLastName, lastName) &&
// ...
) }
after
function OKF(Fn, Datum) { var OK
OK = Fn(Datum.value)
if (!OK) Datum.focus()
return OK }

That is briefer, less repetitive, does much the same, and may need
adjustment.
Then many of the validate functions just test for non-blank : a single
function could be used for all

function nonBlank(Datum, wottitis) { var OK
OK = !isBlank(Datum) // or OK = Datum > "" ?
if (!OK) alert("Enter your ", wottitis, ", please!")
return OK }

Then all of the validate functions could be done with a RegExp for each;
almost all validation can best be done with an initial RegExp format
check.

function ValAny(Datum, wottitis, like, RE) {
OK = RE.test(Datum.value)
if (!OK) alert("Enter your ", wottitis, ", please!\n" +
"It should be " + like)
return OK }

and that approach could be built into OKF.

In fact, all the field-dependent part can be supplied in an array of
arrays

with (Frm) var Chex = [
[firstName, "first name", "non-blank", /./],
[lastName, "last name", "non-blank", /./],

[studentNumber, "Student Number", "ZZ99999", /^[A-Z]{2}\d{5}$/],

[email, ... ]
]

and code as indicated to process each entry. Actually, for a bit more
elegance and typing, make it an array of objects.

For /./ you may prefer /^\S+$/ - no blanks and at least one character.
The simplicity and directness of the RegExp for student number will
eliminate the problem that you are concerned about.

UNTESTED.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Craig Beuker | last post: by
reply views Thread by Gary Shell | last post: by
10 posts views Thread by Eric Lindsay | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.