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

Can a dropdown box default to no choice?

Status
Not open for further replies.

VBGuy2112

Programmer
Feb 19, 2002
58
US
I have a dropdown (select) control with 4 choices. I want to force the user to choose one and not have the first choice appear in the box initially. I know I can add the first option as being blank, but then that blank option appears when they expand the list to choose what they want. Is there a way to have the select box not have an initial value without having a blank as the first choice?

Thanks in advance!

VBGuy2112 (Yes the "2112" is a Rush reference)

"If you've bitten off more than you can chew, chew faster!"
 
This is as close to what you want as I can think of. It doesn't offer your blank option as such, however as an alternative it prompts the user to select an option from the list. The top option (Select...) has no value.

I then add a bit of client side validation so that it doesn't pass it as a value when selected.

Copy and paste this into a page - run it and see if it helps you.

Code:
<html>

<head>
<title>Drop Down Test</title>
</head>

<script language=&quot;Javascript&quot;>
function validate(my_form) {

// --- USER MUST MAKE A SELECTION ---
if (my_form.my_drop_down.value == &quot;&quot;)
 	{
  		alert(&quot;You must make a selection from the options available.&quot;);
 		my_form.my_drop_down.focus();
 		return false;
 	}
}
</script>

<body>

<form method=&quot;post&quot; action=&quot;another_page.asp&quot; name=&quot;my_form&quot; onSubmit=&quot;return validate(my_form)&quot;>
  <p><select size=&quot;1&quot; name=&quot;my_drop_down&quot;>
    <option>Select...</option>
    <option value=&quot;Choice One&quot;>Choice One</option>
    <option value=&quot;Choice Two&quot;>Choice Two</option>
    <option value=&quot;Choice Three&quot;>Choice Three</option>
    <option value=&quot;Choice Four&quot;>Choice Four</option>
  </select><input type=&quot;submit&quot; value=&quot;Submit&quot; name=&quot;submit&quot;></p>
</form>

</body>

</html>

Kind regards,
Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top