Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2012, 06:46 PM   PM User | #1
dalamar17
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
dalamar17 is an unknown quantity at this point
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);
}
}
dalamar17 is offline   Reply With Quote
Old 11-05-2012, 07:17 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 11-05-2012, 07:19 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-05-2012, 07:30 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 11-05-2012, 09:47 PM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-05-2012, 09:53 PM   PM User | #6
dalamar17
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
dalamar17 is an unknown quantity at this point
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);
}
}
dalamar17 is offline   Reply With Quote
Old 11-05-2012, 10:15 PM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
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.
Old Pedant is offline   Reply With Quote
Old 11-05-2012, 10:21 PM   PM User | #8
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Old 11-05-2012, 10:23 PM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-05-2012, 10:25 PM   PM User | #10
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
__________________

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.

Last edited by Philip M; 11-05-2012 at 10:27 PM..
Philip M is offline   Reply With Quote
Old 11-06-2012, 07:44 PM   PM User | #11
dalamar17
New to the CF scene

 
Join Date: Nov 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
dalamar17 is an unknown quantity at this point
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);
}
dalamar17 is offline   Reply With Quote
Old 11-07-2012, 08:06 AM   PM User | #12
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 11-07-2012, 08:19 PM   PM User | #13
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,063 Times in 4,032 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:12 AM.


Advertisement
Log in to turn off these ads.