Click to See Complete Forum and Search --> : javascript select option - help


kbrown2974
12-11-2003, 07:17 AM
I am having some problems validating a select option under Javascript. Below is the code I am having problems with

vbscript to create the selection option
RESPONSE.WRITE "<b>Gender: <select name="&chr(34)&tgender&chr(34)&">"
RESPONSE.WRITE "<option selected value=M>M</option>"
RESPONSE.WRITE "<option value=F>F</option>"
RESPONSE.WRITE "</select>"
RESPONSE.WRITE "<BR>"


javascript to validate it
var checkok = "BAD";
for (var y=0; document.MyForm.tgender.length;y++)
{
if (document.MyForm.tgender[y].selected == true)
{ checkok = "OK"; }
}
if (checkok == "BAD")
{
llok = 0;
alert("Must choose a Gender");
return false;
}

The problem I am having is on the for line. I wanna check to see if the option was clicked on by the user.

Thanks
Ken

Khalid Ali
12-11-2003, 07:27 AM
make sure that VBScript is writing the correct name for select tag

kbrown2974
12-11-2003, 07:31 AM
The name of the select tag is tgender. Is there a quick little one line statement that can be used to see if the select was actually clicked on? For some reason, it doesn't appear in the post data.

Gollum
12-11-2003, 07:39 AM
The usual way to do this is to offer three options to your <select> - the first or default asks the user to select an option and will have some invalid value (say 0) so you can easily see that it hasn't been changed.

<select name=tgender>
<option value=0>M/F
<option value=M>M
<option value=F>F
</select>

kbrown2974
12-11-2003, 09:11 AM
still having a problem with this. If the select box is NOT selected, it is a null object. Any way I can check to see if this way selected? .length does not work, .selected also does not work.

ray326
12-11-2003, 11:17 AM
If the select box is NOT selected, . . .
In this pattern, the invalid choice should be generated as selected.

<select name="foo">
<option value="0" selected="selected">Pick One</option>
<option value="1">First choice</option>
<option value="2">Second choice</option>
</select>