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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Box Validation

Status
Not open for further replies.

Battalion

Programmer
Aug 14, 2002
3
US
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.


 
You code has 2 things.

1st:

<form name=postdata action=?action=add method=post onsubmit='return
checkit()'>

Should be:

<form name=postdata action=&quot;place in your url&quot; method=post onsubmit='return
checkit()'>

2nd:

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

add the bolded code, problem is by default it is returning value 1 to your validate code. To force a choice include an empty selection.

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
I would prefer not to add a blank option... I forgot to put the correct select line in, it should be
<select name=request size=5>
 
No selection in a dropdown returns a selected index
of &quot;-1&quot;...

if(document.postdata.request.selectedIndex == -1){
alert(&quot;Don't make me throw another alert&quot;); }

2b||!2b
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top