HOME BLOG PORTFOLIO PHOTO CONTACT Admin Login
javascript validation on array check box

suppose your checkbox like this

 

<p style="margin-left:20px;">
                     <label><input type="checkbox" name="services[]" id="services" value="Copy Writing"/> Copy Writing</label>
                     <label><input type="checkbox" name="services[]" id="services" value="Brand Development"/> Brand Development</label>
                     <label><input type="checkbox" name="services[]" id="services" value="Press Advertising"/> Press Advertising</label>
                     <label style="margin-left:40px;"><input type="checkbox" name="checkbox13" id="services"  value="Photography"/> Photography </label>
                     <label style="margin-left:8px;"><input type="checkbox" name="services[]" id="services" value="Others" /> Others</label>
                 </p>

 

and you want javascript validation of this array format checkbox

you can do by this way:

var chks = document.form1_feed.elements['services[]'];
var hasChecked = false;
    //alert(chks.length);
    for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
hasChecked = true;
break;
}
}
if (!hasChecked){
alert("Please select at least one.");
chks[0].focus();
return false;
}

 

   Share on Facebook

Page views:758