Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    drop down field validation issue

    Having a problem with this nested validation code. It always wants a value for the region field even when the country drop down is CA or US which is supposed to exclude it. Basically it should only bring up the error message if the region field is empty AND the country drop down is not equal to CA or US. Any help would be greatly appreciated!

    if (document.form1.region.value == "" ){
    if ((document.form1.BillingCountry.selectedIndex != "US") || (document.form1.BillingCountry.selectedIndex != "CA") )
    {
    alert (billingregion_msg);
    region.select();
    region.focus();
    return(false);
    }
    }

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    selectedIndex is the index (position) of the selected item. If you want to check the VALUE of the selectedIndex, you use document.form1.region.options[document.form1.region.selectedIndex].value;
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by WolfShade View Post
    selectedIndex is the index (position) of the selected item. If you want to check the VALUE of the selectedIndex, you use document.form1.region.options[document.form1.region.selectedIndex].value;
    Or simply

    document.form1.BilingCountry.value
    document.form1.region.value

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Quote Originally Posted by Philip M View Post
    Or simply

    document.form1.BilingCountry.value
    document.form1.region.value
    When did they set it so that you could just .value to get the SELECT value? I've always used the method I described.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Quote Originally Posted by WolfShade View Post
    When did they set it so that you could just .value to get the SELECT value? I've always used the method I described.
    When Netscape Navigator 6 died.

    About 2002 or so. (NS7 was released in the second half of 2002.)

    MSIE had always allowed you to use just .value and I remember first finding that, to my annoyance, NS 4 didn't.

    Later browswer--FF and Chrome and Safari (and I think Opera)--have always allowed the use of .value so far as I know.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmmm...neither of those worked. In case I didn't mention, region is a text field and BillingCountry is a list menu.

    Not sure if you meant to put in the whole thing like that...not familiar with the options parameter. I tried also just shortening this to:
    document.form1.[BillingCountry.selectedIndex].value

    but it made no difference

    So now I have this:

    if (document.form1.region.value == "" )
    {
    if ((document.form1.BillingCountry.options[document.form1.BillingCountry.selectedIndex].value != "US") || (document.form1.BillingCountry.options[document.form1.BillingCountry.selectedIndex].value != "CA") )
    {
    alert (billingregion_msg);
    region.select();
    region.focus();
    return(false);
    }
    }

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    bring up the error message if the region field is empty AND the country drop down is not equal to CA or US.
    Well, that last code certainly won't do that!

    No matter what value BillingCountry is, it will *EITHER* be != "US" or != "CA". So your || testing those two values is pointless.

    Code:
    var form = document.form1; // this is obsolescent!
    if ( form.region.value && form.BillingCountry.value != "CA" && form.billingCountry.value != "US" )
    {
        ... error ...
    }
    <form>s should *NOT* use names any more. <form>s should have an id and no name.

    So you should have <form id="form1" ...> and then the first line in that code should be
    Code:
    var form = document.getElementById("form1");
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    Quote Originally Posted by Old Pedant View Post
    Code:
    var form = document.getElementById("form1");
    But isn't form a reserved word?
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Quote Originally Posted by WolfShade View Post
    But isn't form a reserved word?
    No. forms is. But not form.

    But if it makes you nervous, use frm or Form or just f
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Quote Originally Posted by dalamar17 View Post
    Hmmm...neither of those worked. In case I didn't mention, region is a text field and BillingCountry is a list menu.

    Not sure if you meant to put in the whole thing like that...not familiar with the options parameter. I tried also just shortening this to:
    document.form1.[BillingCountry.selectedIndex].value

    but it made no difference

    So now I have this:

    if (document.form1.region.value == "" )
    {
    if ((document.form1.BillingCountry.options[document.form1.BillingCountry.selectedIndex].value != "US") || (document.form1.BillingCountry.options[document.form1.BillingCountry.selectedIndex].value != "CA") )
    {
    alert (billingregion_msg);
    region.select();
    region.focus();
    return(false);
    }
    }

    The selected index is a number 0,1,2,3 4 etc. giving the index of the selected option. That is, the 0th, 1st, 2nd etc. option in the list.

    You want the value of the option. Once more time:-

    form.BillingCountry.value

    (where form is defined as var form = document.getElementById("form1"); )
    As Old Pedant says, form is not a reserved word, but I agree it is a good idea to use frm or f.
    Last edited by Philip M; 11-05-2012 at 11:27 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok I have tried what you said Phillip but it still doesn't co-operate. I have to change the country to get the region field to appear ( i have it hidden unless outside US/Can) and enter something then its fine and will pass validation. But right now only people outside of Can/US can enter their info.

    Of course if I remove the validation altogether its fine and checks the rest of the form as per normal.

    Here's the statement I am using (i reversed it from what it was and made no difference). I took out the name attribute on my form and added the id="form1" and in the top of my validation function I have this:
    var form = document.getElementById("form1");

    Its maybe worth mentioning I also have a function in the top for display of the region field so it only shows up if they select a non US/Can country:
    function myFunction(myValue) {
    var el = document.getElementById("regionRow");
    if ((myValue == "US") || (myValue == "CA")) {
    // form.region.disabled = true;
    el.style.display = "none";
    }
    else {
    //form.region.disabled = false;
    el.style.display = "";
    }
    }


    Here is the main validation section I am using.

    if (((form.BillingCountry.value != "US") || (form.BillingCountry.value != "CA")) && (form.region.value == "" ))
    {
    alert (billingregion_msg);
    return(false);
    }

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,239
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    return(false);
    should be return false;
    Otherwise it is trying to return the value of a variable named false, which does not exist.

    Where does myValue come from?

    It is hard when you show only a fraction of the relevant code. Show the relevant HTML and the relevant Javascript using code tags please.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,084
    Thanks
    80
    Thanked 4,552 Times in 4,516 Posts
    Quote Originally Posted by Philip M View Post
    return(false);
    should be return false;
    Otherwise it is trying to return the value of a variable named false, which does not exist.
    Not true, Philip. Enclosing ANY expression in parentheses--even one that utilizes the built-in constants of JavaScript--does not change the value of the expression.

    You could even do
    Code:
         return ( ( ( ( ( false ) ) ) ) );
    and it would do and mean the same thing.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •