...

View Full Version : drop down field validation issue



dalamar17
11-05-2012, 06:46 PM
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);
}
}

WolfShade
11-05-2012, 07:17 PM
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;

Philip M
11-05-2012, 07:19 PM
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

WolfShade
11-05-2012, 07:30 PM
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.

Old Pedant
11-05-2012, 09:47 PM
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.

dalamar17
11-05-2012, 09:53 PM
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);
}
}

Old Pedant
11-05-2012, 10:15 PM
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.



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


var form = document.getElementById("form1");

WolfShade
11-05-2012, 10:21 PM
var form = document.getElementById("form1");

But isn't form a reserved word?

Old Pedant
11-05-2012, 10:23 PM
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

Philip M
11-05-2012, 10:25 PM
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.

dalamar17
11-06-2012, 07:44 PM
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);
}

Philip M
11-07-2012, 08:06 AM
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.

Old Pedant
11-07-2012, 08:19 PM
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

return ( ( ( ( ( false ) ) ) ) );

and it would do and mean the same thing.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum