Validating Text box with multiple variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark

    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="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>


  • Lee

    #2
    Re: Validating Text box with multiple variables

    Mark said:
    [color=blue]
    >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
    > }[/color]

    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.

    Comment

    • David Leverton

      #3
      Re: Validating Text box with multiple variables

      Mark wrote:[color=blue]
      > 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
      > }[/color]

      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".

      Comment

      • Dr John Stockton

        #4
        Re: Validating Text box with multiple variables

        JRS: In article <NqQqb.671$tP4. 523@newsfep3-gui.server.ntli .net>, seen
        in news:comp.lang. javascript, Mark <not@chance.com > posted at Fri, 7 Nov
        2003 17:15:30 :-
        [color=blue]
        >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![/color]

        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 validateStudent Number, 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.


        [color=blue]
        >function validateForm(fo rm) // validate Survey data
        > {[/color]
        [color=blue]
        > if (!validateFirst Name(form.first Name.value)) // first name valid?
        > {
        > form.firstName. focus()
        > return false
        > }[/color]
        [color=blue]
        >if (!validateLastN ame(form.lastNa me.value)) // last name valid?
        > {
        > form.lastName.f ocus()
        > return false
        > }[/color]


        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(Fr m) {
        with (Frm) return (
        OKF(validateFir stName, firstName) &&
        OKF(validateLas tName, 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.v alue)
        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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...