By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Select Box Validation

P: n/a
I am having issues with making sure users select an option from a select
box... here is my code

function checkit(){
if (document.postdata.phonenumber == ""){
alert("Please enter a Phone Number");
return false;}
if (document.postdata.request[document.postdata.request.selectedIndex].value
== ""){
alert("Please Select a Request");
return false;}
}


<form name=postdata action=?action=add method=post onsubmit='return
checkit()'>
<input type=text name=phonenumber>
<select name=request>
<option value=1>Send manual</option>
<option value=2>Duplicate Product</option>
<option value=3>Did not order</option>
</select>
<input type=submit>
</form>

can someone help me figure this out. I believe it is because
document.postdata.request is undefined until an option is chosen but I don't
know how to fix that.

Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Brian

Check for a selectedIndex value of -1. If no item is selected in a
Select object then:
document.formName.selectObjectName.selectedIndex
will return -1.

Hope that helps

Jul 20 '05 #2

P: n/a
Lee
Brian Kramer said:

I am having issues with making sure users select an option from a select
box... here is my code

function checkit(){
if (document.postdata.phonenumber == ""){
alert("Please enter a Phone Number");
return false;}
if (document.postdata.request[document.postdata.request.selectedIndex].value
== ""){
alert("Please Select a Request");
return false;}
}
can someone help me figure this out. I believe it is because
document.postdata.request is undefined until an option is chosen but I don't
know how to fix that.


No, the problem is that the value of selectedIndex is -1 until an option
is chosen. and document.postdata.request[-1].value is undefined.
That should be all you need to know to fix it.

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.