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 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to validate a multiple selection select box

    Okay.... I got this JavaScript:

    Code:
    function submitform()
    {
    	switch (document.sessions.options.value)
    	{
    		case "total_sess":
    			if (document.pmap_list.options.value == "")
    			{
    				alert('Please select a P-map');
    				return false;
    			}
    			break;
    	      case "total_country": break;
    	      case "total_reseller": break;		
    	      case "total_subs": break;
    	      default:
    	}
    	
    	document.webstats.submit();
    }
    Validating this control:

    Code:
    <select name="pmap_list[]" class="dfont10" size=5 multiple>
                      <option value="1">1</option>
                      <option value="2">2</option>
                      <option value="3">3</option>
                      <option value="4">4</option>
    </select>
    Depending on what is selected here:

    Code:
    <td colspan="2">  
                        <input type="radio" name="sessions" value="total_sess" {sess_sel}>
                        Total Number of Sessions<br>
                        <input type="radio" name="sessions" value="total_country" {country_sel}>
                        Sessions By Country<br>
                        <input type="radio" name="sessions" value="total_reseller" {reseller_sel}>
                        Sessions By Resellers<br>
                        <input type="radio" name="sessions" value="total_subs" {subs_sel}>
                        Session By Independant Subscribers<br>
                        <input type="radio" name="sessions" value="top10" {top10_sel}>
                        Top 10
    </td>
    Basically, I just wanna ensure that a "pmap_list" value is selected. The name must remain pmap_list[] as I am passing this as an array to the php page.
    Last edited by markhartnady; 05-31-2004 at 03:22 PM.

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use array notation instead:
    Code:
    if(document.forms[0]['pmap_list[]'].options.value == "")
    Assuming the element is contained within the first form in the documnt.

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    It should be:

    if(document.forms[0]['pmap_list[]'].selectedIndex == -1)

    But the checking for selected radio button is also wrong.

    Should be:
    Code:
    function submitform()
    {
        var f = document.webstats;
        for (var i=0;i<f.sessions.length;i++)
        {
            if (f.sessions[i].checked)
            {            
                switch (f.sessions[i].value)
                {
                    case "total_sess":
                        if (f.elements['pmap_list[]'].selectedIndex == -1)
                        {
                            alert('Please select a P-map');
                            return false;
                        }
                        break;
                    case "total_country": break;
                    case "total_reseller": break;        
                    case "total_subs": break;
                    default:
                }
                break; //break from for-loop
            }
        }    
        f.submit();
    }

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops; very good call Glenn. I was more so looking at the syntax as opposed to the logic behind it; huge mistake on my part. Thanks for the correction.


  •  

    Posting Permissions

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