www.webdeveloper.com
Results 1 to 4 of 4

Thread: onClick validate checkboxes

  1. #1
    Join Date
    Jul 2010
    Posts
    11

    onClick validate checkboxes

    Hi, I have number of checkboxes and another checkbox for "Select All"

    I want to check if the user has selected at least one checkbox. Need modification in javascript

    Code:
    <head>
    
    <!--script to check, unckeck all boxes-->
    <script type="text/javascript">
    checked=false;
    function All (holiDay) {
    	var all= document.getElementById('holiDay');
    	 if (checked == false)
              {
               checked = true
              }
            else
              {
              checked = false
              }
    	for (var i =0; i < all.elements.length; i++) 
    	{
    	 all.elements[i].checked = checked;
    	}
          }
    </script>
    
    <!--script to check at least one checkbox selected-->
    <script>
    function Validate(){
    var all=document.getElementById('holiDay');
    for(var i=0;i<all.elements.length;i++){ 
    if(all.elements[i].checked)return true; 
    }
    alert("Please identify what warehouses comply:");  
    return false; 
    }
    </script> 
    </head>
    
    <body>
    <form name="holiDay"....
    ....
    []select All
    
    1. <input type="checkbox"....
    2. ....
    3. ....
    
    </body>
    ...

  2. #2
    Join Date
    Jul 2005
    Location
    USA
    Posts
    912
    For starters, your checkboxes can't all have the same ID. They can have the same name attribute but not ID

  3. #3
    Join Date
    Jul 2010
    Posts
    11
    They are variable ID's, in a logic:iterate of JSP. They should work I assume since the logic iterate will loop into it?

  4. #4
    Join Date
    Jul 2010
    Posts
    11
    I included the script within the function which is called when the button is clicked.

    its not working either

    Code:
    <script language="Javascript">
             
    function doSubmit(){
    	function check_checkboxes() 
    	{ 
    	checked=false;
    	var c = document.getElementsByTagName('INPUT'); 
    	for (var i = 1; i < c.length; i++) 
    	{ 
        	if (c[i].type == 'checkbox') 
        	{ 
          	if (c[i].checked) {
           	return true} 
        	else {alert("Please identify what warehouses comply:");  }
        		} 
      		} 
    	}	 
    	document.holiDay.command.value= 'addingApp';
    	document.holiDay.submit(); 
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles