javascript avoid submit if no box checked -


!hello,

i have number of checkboxes generated php. there validation button. want prevent user valid form without checking @ least 1 checkbox.

i have code

var checkboxes = document.getelementsbyname('date[]'); var btn = document.getelementbyid('submit'); date.onchange = function(){    (var i=0;i<checkboxes.length;i++)    {       if(checkboxes[i].checked)       {          btn.disabled = false;       }        else        {          btn.disabled = true;       }    } } <form> .... <table>     <tr id="{{ cpt }}">         <td><input type="checkbox" class="date" id="date" name="date[]"                             checked value="{{ jdv.day }}"></td>         <td>{{ jdv.day }}</td>    </tr> </table>  </form> 

but work first checkbox!

can me?

thanks

your script set button.disabled = true; every checkbox unchecked... means if check box last, set disabled true in end.

add function after btn.disabled = false;

return true; 

resulting in code:

var checkboxes = document.getelementsbyname('date[]'); var btn = document.getelementbyid('submit'); date.onchange = function(){    (var i=0;i<checkboxes.length;i++)    {       if(checkboxes[i].checked)       {          btn.disabled = false;          return true;       }        else        {          btn.disabled = true;       }    } } 

this stop function once detects first checked checkbox , prevent following ones overriding disabled property of button.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -