JavaScript validation - trouble accessing combobox value

Hello everyone,

I just started web development and it’s a whole different ball game from the client! I did a search on notes.net, and I think this is supposed to work, but… it doesn’t. Can anyone see what I’m doing wrong? (all the other validations where I check for null values are fine.)

I have two fields:

Field1(combobox, default value is “1”)

Choice1|“1”

Choice2|“2”

Field2 is text, and is only required if Field1 == “2”.

I have these lines in the JS Header, in a validations function, at the end of the validations that work:

	if (frm.Field1.options[selectedOption].value == "2"  && frm.Field2.value == "") 

{

	alert ("Please enter a value in Field2.");

	frm.Field2.focus();

	return(false);

}	

frm is defined at the top of the JS Header and initialized in OnLoad (frm = window.document.forms[0]:wink:

Thanks for your time,

Rachel

Subject: JavaScript validation - trouble accessing combobox value

“selectedOption” has no meaning by itself, you need to dot your way from the field to get there:

if (frm.Field1.options[frm.Field1.selectedOption].value == “2” && frm.Field2.value == “”) {

//do stuff

}

Subject: RE: JavaScript validation - trouble accessing combobox value

Hi Stan,

Thanks for your response. Unfortunately, still didn’t work. I see “error on page” at the bottom of the screen. Not sure how else to debug this. All I know is that it’s coming from these few lines, because if I take them out, the “code” works… :slight_smile: Do you see anything else?

Thanks again,

Rachel

Subject: RE: JavaScript validation - trouble accessing combobox value

In your original post, you say that you set up your options for the combobox like this:

Choice1|“1”

Choice2|“2”

Did you actually type in those double quotes? If so, then the alias value being saved will look like "“1"” or "“2"”, throwing off the value you are checking for. You don’t need to add quotes when specifying choices for a combobox. Those values will be saved as text anyway. Try taking out those double quotes and see if Stan’s suggestion works.

If you didn’t type in those double quotes, then it’s back to the drawing board!

Hope that helps.

Subject: RE: JavaScript validation - trouble accessing combobox value

Hi Wing,

No quotes. I don’t know why I put them in my post. Ken and Stan found the mistake. It was how I was accessing the value.

Thanks,

Rachel

Subject: RE: JavaScript validation - trouble accessing combobox value

R,

Stan has a typo in his code. The following…

if (frm.Field1.options[frm.Field1.selectedOption].value == “2” && frm.Field2.value == “”) {

…should be…

if (frm.Field1.options[frm.Field1.selectedIndex].value == “2” && frm.Field2.value == “”) {

Also, since you’re a newbie JavaScript coder, it is not a good idea to leave out relevant code. What is frm? Am I supposed to assume you properly set this up as…

var frm = document.forms[0];

…?

Lastly, this statement is wrong…

return(false);

It should be…

return false;

Ken

Subject: RE: JavaScript validation - trouble accessing combobox value

Indeed – I just pasted what Rachel already had and added the reference chain for selectedBlurryBit. I should have checked to see that selectedBlurryBit was the right BlurryBit. Mea culpa.

Subject: RE: JavaScript validation - trouble accessing combobox value

Stan, thanks so much for your help again (you’ve helped me so many times in the past). It works now.

Have a great day,

Rachel

Subject: RE: JavaScript validation - trouble accessing combobox value

Ken, thanks very much! That was it.

Rachel

Subject: JavaScript validation - trouble accessing combobox value

And you’ll probably have to compare against the visible value instead of the alias value.

Collin

Subject: RE: JavaScript validation - trouble accessing combobox value

Hi Collin,

I was hoping to be able to use the alias, but I tried .text too. Didn’t work. Thanks though.

Rachel

Subject: RE: JavaScript validation - trouble accessing combobox value

Domino should be feeding the alias as the .value and the “visible value” as the .text of the option. (One may have to go spelunking for the text node and its nodeData in some browsers, but it shouldn’t be the case for the mainstream stuff.)

Subject: If you really had: Choice1|“1” change it to Choice1|1 instead.