Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
JavaScript
//check option
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:
HTML
<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>
Posted
Updated 5-Feb-14 10:17am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900