Click to See Complete Forum and Search --> : Using JavaScript To Control Forms - Part 2


Milcoi
12-11-2002, 08:41 AM
I found this script and it works fine, now I have 1 question :

I now use 0/1/2/3/4 see script

<HTML>
<HEAD>
<TITLE>Required Select Field</TITLE>
<SCRIPT>
function validate() {
if (mainform.Salary.options[0].selected) {
alert('Please choose a Salary Range.');
event.returnValue=false;
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="mainform" ACTION="http://hoohoo.ncsa.uiuc.edu/cgi-bin/post-query" METHOD="POST" onsubmit="validate();">
<SELECT NAME="Salary">
<OPTION VALUE="0" SELECTED>Salary Range</OPTION>
<OPTION VALUE="1" >Less Than $10,000</OPTION>
<OPTION VALUE="2" >$10,000-$20,000</OPTION>
<OPTION VALUE="3" >$20,000-$30,000</OPTION>
<OPTION VALUE="4" >More than $30,000</OPTION>
</SELECT>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>

Is it also possible to use A/B/C/D/E ??
When I try it is not working.

Thanks,
Milcoi

gil davis
12-11-2002, 09:06 AM
The VALUE attribute of the OPTION has nothing to do with it's SELECT array index. The SELECT array index is always an ordinal number that correlates to the OPTION's position in the HTML.

If you wanted to index the array by specifying a constant (A/B/C/D/E), you should be able to add a NAME attribute to the OPTION tag:

<OPTION NAME="A" ... >

Then you could use this syntax to test it:

if (mainform.Salary.options["A"].selected) {

Nevermind. The syntax is supported in the W3C HTML 4.01 specification, but it doesn't work in IE 5.5.

Milcoi
12-11-2002, 09:16 AM
Yep, just tested it and it doesn't work on IE5.5.

Any 1 else have a idea??

Thanks gil davis for you quick response.

regards,
Milcoi

Milcoi
12-11-2002, 11:45 AM
Any 1 a idea?