1

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?

user3117337
  • 213
  • 1
  • 5
  • 17

1 Answers1

0

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.

Atiris
  • 2,613
  • 2
  • 28
  • 42
  • How do i add a jquery script on my form? it doesn't take effect on mine.. i tried copying all the scripts in the fiddle. but it doesn't run like the demo on the fiddle. – user3117337 Jan 23 '14 at 20:13
  • If you correctly load jQuery, you must only add `id="frm"` to your form tag and I think this is sufficient to control form submitting. JQuery script itself copy to tags before end tag of

    .

    – Atiris Jan 23 '14 at 20:19
  • I create a update of my jsfiddle file: http://jsfiddle.net/Gsc4J/2/ Here you see entire script inside HTML file. Only you need is load jQuery. – Atiris Jan 23 '14 at 20:26
  • it still doesn't work.. how to load the jquery sir? Here's what i add to ur fiddle. ` ` – user3117337 Jan 23 '14 at 20:30
  • hm, it looks ok, if you have files on this places (subdirectory js). ok, I created new version of fiddle: http://jsfiddle.net/atiris/Gsc4J/6/ .. you can copy this text from fiddle to any file on disk (using notepad for example), then open it in browser and you will see that form submit values only if one of this three checkboxes are checked. If you check none, two, or all checkboxes, button Submit do nothing. – Atiris Jan 23 '14 at 20:39
  • Doesn't take effect sir. ive seen it in from the online demo. but the fiddle u made doesn't effect sir. But I need atleast one being checked not only one. i copied everything to a notepad from the latest fiddle u did but when i run it still doesn't show. when i click submit nothing happens even if i check one. – user3117337 Jan 23 '14 at 20:45
  • sir? do you have facebook account? can i ask your email? maybe we can chat. it is hard for me to understand. i'm sorry – user3117337 Jan 23 '14 at 20:49