Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: Selectbox

  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Selectbox

    Hi,
    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.

    Balen

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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!");
    }
    }

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    selectedIndex as you said is a number, why did you do picked.value when picked is the value of the selectedIndex? It should be:
    Code:
    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.
    Code:
    if (picked == 0)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •