We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,220 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

JS- validate a dynamic list in JSP

A list of courses is displayed and user has to select 1, so I used radio buttons. The course list vary from one student to another.

If I have more than 1 courses displayed and user selects one, everything is ok, but if I display 1 course, the test is JS seems to not be right...

Validate if radio button(a course) was checked:

function validate2() {
                //validate radio buttons(if a course is selected)
                var n = document.entry2.courses.length;
                ok = 0;

                for(var i = 0; i < n; i++){
                    if(document.entry2.courses[i].checked){
                        ok++;
                        break;
                    }
                }            

                if((document.entry2.ex.checked == true)||
                  (document.entry2.sem.checked == true)||
                  (document.entry2.lab.checked == true)||
                  (document.entry2.pro.checked == true)){
                    ok++;
                }

                if(ok == 2){
                    return true;
                }else{
                    alert("Please select 1 course and at least 1 activity! ok=" +ok);
                    return false;
                }                
            } 



<form action="Result.jsp" method="post" name="entry2" onSubmit = "return validate2()">

                  Please choose a course from the ones
                  <br> where student is assigned:<br><br>

               ...............

                <table border="1" cellspacing="1" cellpadding="8" bgcolor= #EBDDE2> 

                 <%
                 Iterator<String> i = cs.iterator();
                String sCourse = null;

                 while(i.hasNext()){
                   sCourse = i.next();    
                %>

                <tr>
                  <td><%= sCourse %><input type= "radio" name= "courses" value="<%= sCourse %>"></td>
                </tr>

                <%}%>
            </table>

ok is not becoming 1 at the for loop if there is 1 course only
Thank you!

2
Contributors
1
Reply
8 Months
Discussion Span
1 Month Ago
Last Updated
3
Views
brood_snow
Newbie Poster
3 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

If I have more than 1 courses displayed and user selects one, everything is ok, but if I display 1 course, the test is JS seems to not be right...

The solution to the question is found here:

http://bytes.com/topic/javascript/answers/942414-javascript-radio-button-validation

LastMitch
Industrious Poster
4,177 posts since Mar 2012
Reputation Points: 132
Solved Threads: 335
Skill Endorsements: 45

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0816 seconds using 2.69MB