2

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

}
5
  • if ($('checkb :checkbox:checked').length == 0) { alert("You must answer at least one question"); } .. i names the td:checkb return true; Commented May 3, 2012 at 15:08
  • this is the last thing i have tried. I deleted the rest as they didn't work. Commented May 3, 2012 at 15:09
  • I tried looping through them as an array and i think this is what i have to do but dont no how. Commented May 3, 2012 at 15:10
  • 1
    you can cheek this issue .... stackoverflow.com/questions/590018/… Commented May 3, 2012 at 15:14
  • The "Using JavaScript in PHP" requirement makes no sense... Commented May 3, 2012 at 15:54

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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;
}​

1 Comment

this works in jsfiddle, however it does not work for me , thanks for the help though. I tried something very similar tothis myself and it didnt work
0

Give them a unique class, e.g. 'chbValidate', then try if($('.chbValidate:checked').length == 0)

Comments

0
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

 }

Comments

-1

Try this:

if($('input[name="ModelArray[]"]:checked').length == 0) {
    alert('No checkbox is checked');
}

within Your validate function on submit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.