I'm new to JavaScript and need your help. I created a HTML form and tried to validate each input field using JavaScript.
Here is a scenario:
When I entered a value for city, then hit the enter key; cursor jumped to state field. I selected a state; hit the enter key; cursor didn't jump to zip code field. So I pressed TAB key, cursor jumped to ZIP Code field. I entered a value for ZIP Code; hit the enter key; it sent alert message, “Please select a state".If I continued to press TAB Key, I was able to enter value for the remaining fields. But when I clicked Submit button, it sent alert message, "Please select a state". It seemed that the system didn't recognize that a value in the state field was selected. Without form validation, the form works just fine.
I compared the drop-down list validation code posted in this website with my code; I couldn't figure out why my code didn't work. I'm stuck!! Your help is greatly appreciated.
PennyC
Here is my validation code:
function isOption(elm){
var ddl=document.getElementById("elm");
if (ddl == null) return false;
else return true;
}
if (isOption(form.selstate)==false)
{
alert("Please select a state.");
form.selstate.focus();
return false;
}
Here is my form:
<form action="_gdForm/webformmailer.asp" method="post" önSubmit="return
isReady(this)" name="myform">
.....
<td align="right">
<font face="arial" size="2">State/Prov</font></td>
<td align="left">
<select name="state" size="1" id="selstate" />
<option value="-1" selected> </option>
<option value="1">BC</option>
<option value="2">CT</option>
<option value="3">DE</option>
<option value="4">FL</option>
<option value="5">MA</option>
<option value="6">MD</option>
<option value="7">NH</option>
<option value="8">NJ</option>
<option value="9">NY</option>
<option value="10">OH</option>
<option value="11">ON</option>
<option value="12">Other</option>
<option value="13">PA</option>
<option value="14">QC</option>
<option value="15">RI</option>
<option value="16">TX</option>
<option value="17">VA</option>
<option value="18">VT</option>
<option value="19">WV</option>
</select>
</td>
.......
</form>