Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript validation (drop down list) 1

Status
Not open for further replies.

rjseals

Technical User
Nov 25, 2002
63
US
This should be simple but driving me nuts. I have a simple validation function that basically just makes sure values are entered or selected. Here is the function:

Code:
function validate_form()
{
 if ( document.contact_form.cfirst_name.value == "" )
   {
    alert ( "Please fill in a first name." );
    return false;
   }
......
  else if (document.contact_form.state.options[document.contact_form.state.selectedIndex].value =="")
   {
    alert ( "Please select a state." );
    return false;
    }
.......
  else
   {
    return true;
   }
}
Here is the html

Code:
<form name="contact_form" action="step3.php" method="post" onsubmit="return validate_form();"> 
.....
<select name="state">
 <option></option>
 <option>AK</option>
 <option>AL</option>  
 <option>NE</option>
 <option>SC</option>
 <option>TX</option>
 <option>UT</option>
</select>

....
</form>
This works fine in Firefox but not matter what I do I can't get the dropdown box to work in IE7. Whatever I choose the select a state error message comes up.

Any ideas?

Thanks
 
The way you have it - your options don't have values. They have text. You could change it to either give the options a value:
Code:
...
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="NE">NE</option>
...
That would probably be the better solution.

Alternatively you could change your validation to look at the .text - like this
Code:
...
else if
(document.contact_form.state.options[document.contact_form.state.selectedIndex].text =="") {
alert( ...
I'm not sure that would work - but it would reference the text of each option. You could give that a try.


Einstein47
For best results: hand wash in cold, tumble dry low.
For not so good results: drag through puddles, pound on rocks, air dry on tree branch.
[&#91;]Starbase47.com]
 
First option did it. I knew it was easy. Many thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top