Saturday, November 13, 2010

Multiple Checkbox Validation

How to validate the checkbox using Jquery?

In the HTML form have multiple chekcbox in same using html array element.
That time we will use three types of checkbox validation for checked or not.
Checkbox validation has three types.
  • Checkbox validation using Id
  • Checkbox validation using name
  • Checkbox validation using div tag
Example

Checkbox validation using Id

<form>
    <div id="checkboxId">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    <input type="checkbox" name="chkBox[]" id="chkBox[]">
    </div>
    <input type="button" name="Btnaction" value="submit" 
onclick="return validateId();">
</form>
</div>
<script language="javascript" src="jquery.js" type="text/javascript">
</script>
<script language="javascript">
function validateId()
{
 var selector_checked = $("input[@id=chkBox]:checked").length;
    alert(selector_checked);
    if (selector_checked == 0)
    {
        return false;
    }
    else if (selector_checked == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}
</script>

Checkbox validation using Name

function validateName()
{
    var selector_checked = $("input[@name=chkBox]:checked").length;
    alert(selector_checked);
    if (selector_checked == 0)
    {
        return false;
    }
    else if (selector_checked == 0)
    {
        return false;
    }
    else
    {
        return true;
    }
}

Checkbox validation using Div tag

function validateDiv()
{
    if($("#checkboxId input[type='checkbox']:checked").length > 0)
    {
     return true;
    }
}

No comments:

Post a Comment