Pavlos1316 Posted June 14, 2008 Share Posted June 14, 2008 Hi to all (quite new still) I have a form with checkboxes. It works fine. It has 24 columns. 8 rows X 3 colums each. I need when the form is submited to check if: 1. every row has at least 1 column checked 2. there are no more than 9 columns checked. That means one column checked in every row exept one that will have 2 columns checked. Can anyone help me? Thanks P.S. cause until now I use only php where do I place javascript code? (begining, end?) Quote Link to comment https://forums.phpfreaks.com/topic/110221-form-with-checkboxes/ Share on other sites More sharing options...
hansford Posted June 14, 2008 Share Posted June 14, 2008 javascript code will be within the html. when the function has finished it will submit the form. You should post your form, so maybe someone will take the time to show you the necessary javascript function Quote Link to comment https://forums.phpfreaks.com/topic/110221-form-with-checkboxes/#findComment-565583 Share on other sites More sharing options...
Psycho Posted June 15, 2008 Share Posted June 15, 2008 <html> <head> <script language="Javascript" type="text/javascript"> function checkForm(formObj){ var double = false; for (var row=1; row<=8; row++) { var rowCount = 0; for (var col=1; col<=3; col++) { if (formObj['chk-'+row+'-'+col].checked) { rowCount++; } } if (rowCount==0) { alert('You must select at least one checkbox from each row'); return false; } if (rowCount>2) { alert('You may not select more than two checkboxes from any row'); return false; } if (rowCount==2) { if (double) { alert('You may not select two checkboxes in more than one row'); return false; } else { double = true; } } } if (!double) { alert('You must select two checkboxes in one of the rows'); return false; } return true; } </script> </head> <body> <form name="theform" onsubmit="return checkForm(this);"> <table border="1"> <tr> <td> </td> <td>1</td> <td>2</td> <td>3</td> </tr> <tr> <td>1</td> <td><input type="checkbox" name="chk-1-1"></td> <td><input type="checkbox" name="chk-1-2"></td> <td><input type="checkbox" name="chk-1-3"></td> </tr> <tr> <td>2</td> <td><input type="checkbox" name="chk-2-1"></td> <td><input type="checkbox" name="chk-2-2"></td> <td><input type="checkbox" name="chk-2-3"></td> </tr> <tr> <td>3</td> <td><input type="checkbox" name="chk-3-1"></td> <td><input type="checkbox" name="chk-3-2"></td> <td><input type="checkbox" name="chk-3-3"></td> </tr> <tr> <td>4</td> <td><input type="checkbox" name="chk-4-1"></td> <td><input type="checkbox" name="chk-4-2"></td> <td><input type="checkbox" name="chk-4-3"></td> </tr> <tr> <td>5</td> <td><input type="checkbox" name="chk-5-1"></td> <td><input type="checkbox" name="chk-5-2"></td> <td><input type="checkbox" name="chk-5-3"></td> </tr> <tr> <td>6</td> <td><input type="checkbox" name="chk-6-1"></td> <td><input type="checkbox" name="chk-6-2"></td> <td><input type="checkbox" name="chk-6-3"></td> </tr> <tr> <td>7</td> <td><input type="checkbox" name="chk-7-1"></td> <td><input type="checkbox" name="chk-7-2"></td> <td><input type="checkbox" name="chk-7-3"></td> </tr> <tr> <td>8</td> <td><input type="checkbox" name="chk-8-1"></td> <td><input type="checkbox" name="chk-8-2"></td> <td><input type="checkbox" name="chk-8-3"></td> </tr> </table> <button type="submit">Submit</button> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/110221-form-with-checkboxes/#findComment-566074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.