1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Rep Power

    JavaScript help needed on && ||

    I'm validating a form and having problems with the && and ||.
    My form has:
    a radio button for determing what country: there are three radio butttons all with the same name
    NAME="country" , the first radio button is checked by default and it's value is USA (the other 2 values are CANADA and OTHER)

    a province text box:

    and a text for another country (if USA or CANADA isn't checked)

    *****MY PROBLEM********
    I do not want validate the bill_province or bill_country_other text box, when the "country" radio button is set to USA (the first of three values). My problem is that it doesn't seem the && or || are working correctly.

    Here's the piece of code that's not working:

    Loop through the elements on the form, then assing an object as the field on the form.....
    var fieldOrderProcObj = varFormName.elements[i];

    then test for a non USA country AND the control's name should be either bill_province OR bill_country_other......

    if((document.forms[1].bill_country[0].checked == false) && ((fieldOrderProcObj.name == "bill_province") ||(fieldOrderProcObj.name == "bill_country_other")) )


    perform test to see if these 2 fields have value....

    Again the code should say
    IF the first of three radio buttons named bill_country is not checked AND
    (( the field being tested is named bill_province) OR
    (the field being tested is named bill_country_other)) then
    perform a test to see if the field being tested has a value

    Wow.. Quite honestly it seems C or C++ is more sensible than JavaScript sometimes...

    I've double checked the control names and everything
    Any help would be appreciated

    ~Just Say When ~
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    New York
    Rep Power
    If I were in your shoes, I would simply your conditional tests. Try the following skeletal code:

    <script language="Javascript">
    function formChecker() {
    if(document.form.bill_country[0].checked == true) {
    //do something here for US customers....
    } else if(document.form.bill_country[1].checked == true && document.form.elements.bill_province.value == "") {
    alert("Please provide your province");
    return false;
    } else if (document.form.bill_country[2].checked == true && document.form.elements.bill_country_other.value == "") {
    alert("Please provide your country");
    return false;

    It should validate a form like the following in the manner you want.

    <form name="form" onSubmit="return formChecker()">
    USA<input type="radio" name="bill_country" value="usa" checked>
    Canada<input type="radio" name="bill_country" value="can">
    Other<input type="radio" name="bill_country" value="other">
    province:<input type="text" name="bill_province">
    country:<input type="text" name="bill_country_other">
    <input type="submit">

    Good luck.

IMN logo majestic logo threadwatch logo seochat tools logo