Click to See Complete Forum and Search --> : prompt for drop down menu?


kahlua17
11-21-2003, 12:22 PM
I have the following code to detect if an input box is blank, and prompts the user to enter in text. What I would like to find out is how to modify it to detect if an option from a dropdown box has been selected.

<script language="JavaScript">
<!-- Begin

function noEntry(){
a2=document.form.Option.value;

if((a2.length<1)||(a2.substring(0,6)=="******")){
alert("Please select an option");
document.form.Option.focus();
return false;
}
else { return true; }
}

//End -->
</script>

gil davis
11-21-2003, 12:36 PM
If you have a single select (not declared "multiple") then no selection has been made if the selectedIndex is -1. Another way would be to set "defaultSelected" on the top option, then if no selection is made, "selectedIndex" should be 0.

Choose your poison.

gil davis
11-21-2003, 12:39 PM
Originally posted by kahlua17
a2=document.form.Option.value;This would only work in IE. Proper access of the selected value would be
a2=document.form.Option.options[document.form.Option.selectedIndex].value;

kahlua17
11-21-2003, 02:46 PM
If you have a single select (not declared "multiple") then no selection has been made if the selectedIndex is -1. Another way would be to set "defaultSelected" on the top option, then if no selection is made, "selectedIndex" should be 0.

now you've lost me. Where would I enter that? (I am a newbie at JavaScript)

gil davis
11-21-2003, 03:13 PM
When you show someone some JavaScript without showing them the form, it is sort of like asking someone to sell you tires without telling him what kind of car you have.

I was explaining two possibilities for problems with a SELECT object without seeing your HTML.

<select ...>
<option value="whatever" selected>-- Please select from the list ---</option>
<option>This is a valid selection</option>
<option>So is this</option>
</select>In this example, the selectedIndex would be 0 if nobody changes the drop down.
<select ...>
<option>-- Please select from the list ---</option>
<option>This is a valid selection</option>
<option>So is this</option>
</select>In this example, the selectedIndex would be -1 if nobody changes the drop down.