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!
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".
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;
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".
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
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.
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".
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
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.
__________________
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.
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 = "";
}
}
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.