JSP form with checks

brianmadden's Avatar, Join Date: Sep 2012
Newbie Member
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

<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">
They are then brought to another page that thanks them for choosing those options which I have stored in array.

  <p><b> Thank you <u><%= session.getAttribute( "username" ) %> </u>for registering </b></p>
                  <%String[] selections = request.getParameterValues("id");
                    if (selections != null && selections.length != 0) {
                 out.println("You have selected: ");
                for (int i = 0; i < selections.length; i++) {
brianmadden's Avatar, Join Date: Sep 2012
Newbie Member
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

  <script type="text/javascript">
function validate(form) {

var total=""
for(var i=0; i < 2; i++){
total +=document.form1.id[i].value + "\n"
alert("Please select an option")
alert ("The options you have selected are: "+"\n"+total)

return false;
} </script>