Checkbox validation in javascript


Checkbox and radio button validation in javascript:

<script>
function validateCourse(){
count = 0; gend = 0;
len = document.testForm.elements.length;
    for(i=0;i<len;i++){
elt = document.testForm.elements[i];
if(elt.type == 'checkbox' && elt.name=='course' && elt.checked){
    count++;
}        
if(elt.type == 'radio' && elt.name=='gender' && elt.checked){
    gend++;
}
    }
    if(count<3){
        alert("Please Select 3 Courses");
    }
    if(gend == 0){
        alert("Select Gender Field");
    }
    }
</script>
<form name='testForm'>
<big> Subject You Know</big><br>
<input type='checkbox' name='course' value='c'> C-Language <br>
<input type='checkbox' name='course' value='j' > Joomla <br>
<input type='checkbox' name='course' value='p' > PHP <br>
<input type='checkbox' name='course' value='s' > SAP <br>
<input type='checkbox' name='course' value='d' > Drupal <br>
<input type='checkbox' name='course' value='m' > MySQL <br><br>
<input type='radio' name='gender' value='m'> Male <input type='radio' name='gender' value='f'> Female <br><br>
<input type='button' name='coursecount' value='Validate !' onclick="javascript:validateCourse();">
</form>

Demo:

[gickr.com]_5913b045-5f87-a0b4-8dc4-079f7bfe7f1a

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s