go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Form field validation - verify checkbox using JavaScript
 
Subject: Form field validation - verify checkbox using JavaScript
Author: WebSpider
Posted on: 08/01/2006 09:58:56 PM

It is very common to see if a checkbox has been checked or not before a form being sent out to server. This can be done by the following javascript code

<script language="JavaScript">
function verify_checkbox(checkbox)
{
   var isChecked = false;
   if checkbox[0]) { /* chackbox group/array */
	
      for(var j=0; j<checkbox.length; j++){
         if(checkbox[j].checked){
	    alert("The checkbox #"+(j+1)+"has been checked.");
            isChecked = true;
         }
      }
   }else{
      if(checkbox.checked){
         alert("The checkbox has been checked.");
	 isChecked = true; 
      }
   }

   if(!isChecked)
   	alert("No checkbox has been checked.");

}
</script>

What we have learned are:
  • The attribute 'checked' of checkbox plays a key role;
  • There could be an array of checkboxes bearing the same group name.


    Replies:


    References:

  •  


     
    Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.