Reputation: 267
I am trying to validate checkboxes in a form.
my form is as follows:
<form name="results" action=request_job.php method="post"
onsubmit="return validateForm.apply(this)">
<td name='checkb'colspan='2' class="noborder" width='100%' align="center">
<input type='checkbox' name='ModelArray[]' value='1'>m1
<input type='checkbox' name='ModelArray[]' value='2'>m2
<input type='checkbox' name='ModelArray[]' value='3'>m3
<input type='checkbox' name='ModelArray[]' value='4'>m4
<input type='checkbox' name='ModelArray[]' value='5'>m5
<input type='checkbox' name='ModelArray[]' value='6'>m6
<input type='checkbox' name='ModelArray[]' value='7'>m7
I want to there to be an alert if there is no checkboxes selected. How do i do this. I have being trying a long time now.
Thanks
solved with this:
var wrap=doc.get("checkb");
ArrCB=wrap.getelementbyTagName('input');
ArrCB_l=ArrCB.lenght();
while(ArrCB_l--){
var CB=ArrCB[ArrCB_l];
CB.checked()==True;
return 1
}
return 0
}
Upvotes: 2
Views: 7839
Reputation: 267
var wrap=doc.get("checkb");
ArrCB=wrap.getelementbyTagName('input');
ArrCB_l=ArrCB.lenght();
while(ArrCB_l--){
var CB=ArrCB[ArrCB_l];
CB.checked()==True;
return 1
}
return 0
}
Upvotes: 0
Reputation: 3078
I would do it the following (quite clean - pure browser js) way (and tried this on jsfiddle with success)
function validateForm() {
var i, chks = document.getElementsByName('ModelArray[]');
for (i = 0; i < chks.length; i++)
if (chks[i].checked)
return true;
alert('No value selected');
return false;
}
Upvotes: 1
Reputation: 3730
Give them a unique class, e.g. 'chbValidate', then try if($('.chbValidate:checked').length == 0)
Upvotes: 0
Reputation: 1119
From whatever information I could gather from your query, I have posted a solution. See if this works for you.
<form name="results" action="request_job.php" method="post" onsubmit="return validateForm()">
<input type='checkbox' name='ModelArray[]' value='1'>m1
<input type='checkbox' name='ModelArray[]' value='2'>m2
<input type='checkbox' name='ModelArray[]' value='3'>m3
<input type='checkbox' name='ModelArray[]' value='4'>m4
<input type='checkbox' name='ModelArray[]' value='5'>m5
<input type='checkbox' name='ModelArray[]' value='6'>m6
<input type='checkbox' name='ModelArray[]' value='7'>m7
<input type="submit" value="Submit">
</form>
<script type="text/javascript">
function validateForm()
{
if( $('input[name="ModelArray[]"]:checked').length == 0 )
{
alert("You must check atleast one checkbox");
return false;
}
else
return true;
}
</script>
Let me know if this doesn't work for you.
Upvotes: 1
Reputation: 16065
Try this:
if($('input[name="ModelArray[]"]:checked').length == 0) {
alert('No checkbox is checked');
}
within Your validate function on submit.
Upvotes: -1
Reputation: 24671
You can try this (not dependent on jQuery since you did not specify):
(function() {
window.validateForm = {};
window.validateForm.apply = function(o) {
var inputs = document.getElementsByTagName("input");
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type == "checkbox" && inputs[i].checked)
return true;
}
alert('You must select at least 1');
return false;
};
})();
This will iterate over all inputs in your page. It will return true once it finds a single checked checkbox, or false if it finds none.
I added the window.validateForm = {}
just so that it would work, you probably won't need that if you already have the object defined.
Upvotes: 2