Thursday, 22 August 2013

check atleast one checkbox is checked on form submission

check atleast one checkbox is checked on form submission

I have a form that consists of checkbox fields, now on form submission we
should check whether atleast one checkbox is checked
html code
<form id="form_check" class="form" action="/path/to/some/url" method="POST">
{% for field in fields %}
<div class="check_fields">
<input class="select-unselect" type="checkbox" name="invite"
value="">
{{field}}
</div>
{% endfor %}
<input type="submit" class="btn btn-primary" value="Submit"
onsubmit="atleast_onecheckbox()"/>
</form>
javascript code
<script type="text/javascript">
function atleast_onecheckbox()
{
var value = $("[name=invite]:checked").length > 0);
alert(value) ;
if (!value)
{
alert("Please.....");
}
}
</script>
So when i clicked on the submit button, the form is redirecting to the url
mentioned in the action, but its not even hitting the javascript function
atleast_onecheckbox()
what wrong in the above code, can anyone please make the above code work ?

No comments:

Post a Comment