473,385 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 13972
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: and | last post by:
Hi I have been validating all day most things are cool but I cant get by this problem. One I have listed the script (JAVASCRIPT ) in all the right placesnot a prob but the validator insists...
1
by: Craig Beuker | last post by:
Hello, I am experimenting with this XmlValidatingReader and have a question about how it is working (or not working as would be the case) The sample documents and code are included at the end...
0
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
0
by: Gary Shell | last post by:
I am experiencing some strange behavior between a UserControl's validating event and a treeview control. Initially, I thought it was related to an issue in the Knowledgebase article 810852...
10
by: Eric Lindsay | last post by:
How are people are validating multiple web pages? I thought most of my pages were valid so as a first try I used curl -F uploaded_file=@index.htm\;type=text/html http://validator.w3.org/check...
1
by: cemcat | last post by:
Hello, We have an ASP.NET 2.0 (C#) web form that contains a textbox for users to enter multiple e-mail addresses separated by semicolons. We need to validate that each individual e-mail address...
8
by: Peted | last post by:
I have an amazing problem which i think i have no hope of solving Im working with a c# dot net module that is hosted by and runs under a delphi form envrioment. Dont ask me how this insanity has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.