Go4Expert (http://www.go4expert.com/)
-   JSP (http://www.go4expert.com/forums/jsp/)
-   -   JSP form with checks (http://www.go4expert.com/forums/jsp-form-checks-t29121/)

brianmadden 20Sep2012 04:58

JSP form with checks
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 20Sep2012 15:04

Re: JSP form with checks
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>

All times are GMT +5.5. The time now is 05:56.