Hello, I am trying to create a jsp form with 4 options the user can only select 2 of the four options and are blocked from selecting more and given a prompt if they try to. I am not sure how to create a check to see how many checkboxes they have used, I think I should be using an array and a loop but I am struggling to find a solution Code: <form ACTION="page2.jsp"> Opt 1 <input type="checkbox" name="id" value="check1 +"> Opt 2 <input type="checkbox" name="id" value="check2 +"> Opt 3 <input type="checkbox" name="id" value="check3 +"> Opt 4 <input type="checkbox" name="id" value="check4 +"> <input type="submit" value="Submit"> </form> They are then brought to another page that thanks them for choosing those options which I have stored in array. Code: <p><b> Thank you <u><%= session.getAttribute( "username" ) %> </u>for registering </b></p> <p> <%String[] selections = request.getParameterValues("id"); session.setAttribute("id",selections); if (selections != null && selections.length != 0) { out.println("You have selected: "); for (int i = 0; i < selections.length; i++) { out.println(selections[i]); } } %>
I have been searching for a few check box solutions, and think I need to write an method that runs when the user presses the submit button Code: <script type="text/javascript"> function validate(form) { var total="" for(var i=0; i < 2; i++){ if(document.form1.id[i].checked) total +=document.form1.id[i].value + "\n" } if(total==""){ alert("Please select an option") } else alert ("The options you have selected are: "+"\n"+total) return false; } </script>