473,466 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

JavaScript - radio button validation

sunsolaris2000
20 New Member
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:


Expand|Select|Wrap|Line Numbers
  1. function validate2() {
  2.                 //validate radio buttons(if a course is selected)
  3.                 var n = document.entry2.courses.length;
  4.                 ok = 0;
  5.  
  6.                 for(var i = 0; i < n; i++){
  7.                     if(document.entry2.courses[i].checked){
  8.                         ok++;
  9.                         break;
  10.                     }
  11.                 }            
  12.  
  13.                 if((document.entry2.ex.checked == true)||
  14.                   (document.entry2.sem.checked == true)||
  15.                   (document.entry2.lab.checked == true)||
  16.                   (document.entry2.pro.checked == true)){
  17.                     ok++;
  18.                 }
  19.  
  20.                 if(ok == 2){
  21.                     return true;
  22.                 }else{
  23.                     alert("Please select 1 course and at least 1 activity! ok=" +ok);
  24.                     return false;
  25.                 }                
  26.             } 
  27.  
Expand|Select|Wrap|Line Numbers
  1. <form action="Result.jsp" method="post" name="entry2" onSubmit = "return validate2()">
  2.  
  3.                   Please choose a course from the ones
  4.                   <br> where student is assigned:<br><br>
  5.  
  6.                ...............
  7.  
  8.                 <table border="1" cellspacing="1" cellpadding="8" bgcolor= #EBDDE2> 
  9.  
  10.                  <%
  11.                  Iterator<String> i = cs.iterator();
  12.                 String sCourse = null;
  13.  
  14.                  while(i.hasNext()){
  15.                    sCourse = i.next();    
  16.                 %>
  17.  
  18.                 <tr>
  19.                   <td><%= sCourse %><input type= "radio" name= "courses" value="<%= sCourse %>"></td>
  20.                 </tr>
  21.  
  22.                 <%}%>
  23.             </table>
ok is not becoming 1 at the for loop if there is 1 course only
Thank you!
Aug 16 '12 #1

✓ answered by gits

in case that this one option isn't 'checked' then it will not be counted - or what is the exact problem? can you post the html as it turns out in the browser instead of the jsp-page? may be you want to auto-select the option when there is only one available?

2 2194
gits
5,390 Recognized Expert Moderator Expert
in case that this one option isn't 'checked' then it will not be counted - or what is the exact problem? can you post the html as it turns out in the browser instead of the jsp-page? may be you want to auto-select the option when there is only one available?
Aug 20 '12 #2
sunsolaris2000
20 New Member



ok must be 2 for the form to be submitted.
And I get ok=1 only when I have 1 course.
Aug 24 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.