Need help in Select Box + Javascript

Discussion in 'JavaScript' started by tamilsoft, Jun 3, 2008.

  1. #1
    Hi,
    I have a the below select box:
    <form name="test" method="post">
    <select name="myoptions[]" multiple>
    <option value="IN">India</option>
    <option value="US">United States</option>
    <option value="UK">United Kingdom</option>
    <option value="PK">Pakistan</option>
    </select><BR>
    <input type="submit" value="show" onClick="docheck();">
    </form>
    HTML:
    If a user do not select at least one option, we should raise an alert message?, Can anyone help me on this?

    thanks in advance
    tamilsoft
     
    tamilsoft, Jun 3, 2008 IP
  2. yourwish

    yourwish Active Member

    Messages:
    43
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    98
    #2
    if(document.test.elements['myoptions[]'].selectedIndex==-1){
    alert("Please select atleast one type.");
     
    yourwish, Jun 3, 2008 IP
    greatlogix likes this.
  3. greatpree

    greatpree Guest

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    Write down following javascript in head part
    function check(b)
    {


    for (var c=0,d=0,e; e=b[d]; d++)
    {
    if (e.type=='select-multiple' && e.name.match(/^myoptions\[\]$/) && e.selectedIndex >= 0 ) c++;
    }
    if(c==0)
    {
    alert("Select at least one");
    return false;
    }
    return true;
    }

    You have to call this function like following
    <form name='frm1' action='#' method="post" onsubmit="return check(this);">
    .... your code here....
    </form>
     
    greatpree, Jun 3, 2008 IP