简体   繁体   中英

Html5 required validation checkbox

I made a input checkbox that contains array values. so it generates plenty of rows in a table. But it needs for me to check it all to submit. It doesn't allow me to check only few not all.

<form>
<table>

<td>
<input required="required" type="checkbox" name="id[]" id="id" value="<?php  echo $result2["id"]; ?>"/>
</td>

<input type="submit" name="submit" value="submit"/>

</table>
</form>

How can i make the required field able to check atleast one and able to submit even if not all are checked?

I dont know if I understand you,
but maybee you need something like this:
Online demo

Main part with notes:

$('#frm').bind('submit', function(e) { // set this function for submit for your formular
    validForm = true; // default value is that form is correct, you can chcange it as you wish
    var n = $( "input:checked" ).length; // count of checked inputs detected by jQuery
    if (n != 1) { validForm=false; } // only if you checked 1 checkbox, form is evaluated as valid
    if (!validForm) { // if result of validation is: invalid
        e.preventDefault(); // stop processing form and disable submitting
    }
    else {
        $('#echo').html("OK, submited"); // ok, submit form
    }
});

You can use any number of comboboxes under any tag and get count of selected by jQuery, then use any rule for validate form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM