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

Select Box 1

Status
Not open for further replies.

JohannIcon

Programmer
Sep 3, 2002
440
MT
I have a select box, which is retreiving items from a database. I want this select box initial value to blank and index of 0 like this:-

Response.Write (&quot;<OPTION VALUE=&quot;&quot;0&quot;&quot;></OPTION>&quot;)

Now how can I check that the user, after filling in the form, does not leave the select box empty but chooses an item from the list? I can remove this option and display the items directly, however I wish that the initial form is blank so that the user does not make a mistake and chooses the first option automatically or by mistake.

Thanks for your help
 
Give your select an id (for exemeple &quot;mySelect&quot;) and then, code this in your validation function :
Code:
var oSel = document.getElementById(&quot;mySelect&quot;);
if (oSel.selectedIndex==0) or (oSel.selectedIndex==-1)
  alert (&quot;Choose one !!!&quot;);

Note that if only write that :
Code:
Response.Write (&quot;<OPTION VALUE=&quot;&quot;0&quot;&quot;></OPTION>&quot;)
your line won't be selected (oSel.selectedIndex will be -1). If you want to select an option in a select, write that :
Code:
Response.Write (&quot;<OPTION SELECTED VALUE=&quot;&quot;0&quot;&quot;></OPTION>&quot;)
Water is not bad as long as it stays out human body ;-)
 
in Javascript...

if (document.myForm.mySelect.selectedIndex == 0){
alert (&quot;Please select an item from the list&quot;)
} -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
I tried both but they did not work. This is my code:-

<select name=&quot;FilterSelection&quot; id=&quot;MySelect&quot;>

//check to see if select is empty
var oSel = document.getElementById(&quot;mySelect&quot;);
if (oSel.selectedIndex==0) or (oSel.selectedIndex==-1)
{
alert(&quot;Please Choose a package!!&quot;)
return false
}
 
This code seems good to me except for the case of id wich is written &quot;MySelect&quot; at a place and &quot;mySelect&quot; at the other one. Try to correct that and, if it still don't work, post the full page. Water is not bad as long as it stays out human body ;-)
 
Go again in the good thread :
Try that :
Code:
<select name=&quot;FilterSupport&quot; id=&quot;FilterSupport&quot;>
<%
   ' no change to ASP code
%>
</select>

//check to see if support update Text is empty
var oSel = document.getElementById(&quot;FilterSupport&quot;);
if (oSel.selectedIndex==0) 
{
    alert(&quot;Please Choose a package!!&quot;);
    oSel.focus();
    oSel.select();
    return false;
}
if it doesn't work, give me the message and the line on wich the error occurs.
Water is not bad as long as it stays out human body ;-)
 
Oh it worked at last! However without the oSel.select() cause it told me that this object does not support this method or class

Thanks very much mate!
 
Yeah, we got it at last LOL Water is not bad as long as it stays out human body ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top