Validating Dropdown Lists

Question: In a dropdown list, I want to make sure that a choice is selected other than the first one (which asks the user to choose a value), what attributes of the dropdown list do I use to validate this?
Anonymous

Answer: The easiest way to do this is to give the first entry a value of blank and assign non-blank values to the entries that you want selected from. eg:

<form name="myform">
<select name="sel" size="1">
<option value=" ">Please select an option below</option>
<option value="1">Option One</option>
<option value="2">Option Two</option>

Now in your validation you test if the field is blank.

if (document.myform.sel.options[ document.myform.sel.options.selectedIndex ].value == " ")
alert("You must choose an option");

Just substitute the correct names for the form and selection box that you are validating.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate