View Full Version : Selectbox

01-19-2006, 06:08 AM
I need a sample coding for selectbox validation. If the user clicks the button without choosing any item in select box an alert should display. Thank you.


Philip M
01-19-2006, 09:05 AM
An options[] array has a property called selectedIndex that contains the index number of the item currently selected.

function show(){
var picked = document.form.selectName.selectedIndex;
alert (picked);
if (picked.value ==-1) {
alert ("You must select an option!");

01-19-2006, 11:17 AM
selectedIndex as you said is a number, why did you do picked.value when picked is the value of the selectedIndex? It should be:

if (picked == -1)
But if the combobox has no size defined or size is 1, selectedIndex will never be -1 as there will always be a selected item. Usually the first item is a dummy option saying "-Please Select-" to signify no selection. So you should check if selectedIndex is 0 not -1.

if (picked == 0)