Solved

validation on radio button

Posted on 2000-05-15
7
397 Views
Last Modified: 2012-05-04
Hi, I'm newbie in this area.
I have the following code :
I need some validation to all the radio button have to be checked before i post the form.
so If there is one or more radio button hasn't been checked then, the form won't be post.
I thougth, I can do it all with Java Script.

<form name="a" method="post" action="survey.asp">
<TABLE>  
  <TR>
    <TD>
      1. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q1 type=radio value="">abc<br>
      <INPUT name=q1 type=radio value="">xyz<br>
      <INPUT name=q1 type=radio value="">pqr<br>
        <INPUT name=q1 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      2. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q2 type=radio value="">abc<br>
      <INPUT name=q2 type=radio value="">xyz<br>
      <INPUT name=q2 type=radio value="">pqr<br>
        <INPUT name=q2 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      3. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q3 type=radio value="">abc<br>
      <INPUT name=q3 type=radio value="">xyz<br>
      <INPUT name=q3 type=radio value="">pqr<br>
        <INPUT name=q3 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      4. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q4 type=radio value="">abc<br>
      <INPUT name=q4 type=radio value="">xyz<br>
      <INPUT name=q4 type=radio value="">pqr<br>
        <INPUT name=q4 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      5. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q5 type=radio value="">abc<br>
      <INPUT name=q5 type=radio value="">xyz<br>
      <INPUT name=q5 type=radio value="">pqr<br>
        <INPUT name=q5 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      6. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q6 type=radio value="">abc<br>
      <INPUT name=q6 type=radio value="">xyz<br>
      <INPUT name=q6 type=radio value="">pqr<br>
        <INPUT name=q6 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      7. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q7 type=radio value="">abc<br>
      <INPUT name=q7 type=radio value="">xyz<br>
      <INPUT name=q7 type=radio value="">pqr<br>
        <INPUT name=q7 type=radio value="">123
      </TD>
  </TR>
    <TR>
    <TD>
      8. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q8 type=radio value="">abc<br>
      <INPUT name=q8 type=radio value="">xyz<br>
      <INPUT name=q8 type=radio value="">pqr<br>
        <INPUT name=q8 type=radio value="">123
      </TD>
  </TR>
  <TR>
    <TD>
      9. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q9 type=radio value="">abc<br>
      <INPUT name=q9 type=radio value="">xyz<br>
      <INPUT name=q9 type=radio value="">pqr<br>
        <INPUT name=q9 type=radio value="">123
      </TD>
  </TR>
      <TR>
    <TD>
      10. abcde abcde abcde abcde abcde abcde<br>
      <INPUT name=q10 type=radio value="">abc<br>
      <INPUT name=q10 type=radio value="">xyz<br>
      <INPUT name=q10 type=radio value="">pqr<br>
        <INPUT name=q10 type=radio value="">123
      </TD>
  </TR>
</TABLE>
<p>No.KTP : &nbsp;<input type="text" value="" name="id" size="20"></p>
<input type="Submit" value="Submit"><input type="reset" value="Cancel">
</form>


  JavaBoy
0
Comment
Question by:JavaBoy060299
  • 3
  • 3
7 Comments
 
LVL 2

Expert Comment

by:indu_ma
ID: 2809879
I assume that i have two radio buttons under each group of radio button since u have for choice for each radio group use the condidtion for the for..loop as i<4

To get the checked value for the radio buttons under one group we have to use the index of it
Hope this gives u the answer


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">


<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function radio1_onclick() {
for(i=0;i<2;i++)
{
if (a.radio1[i].checked)
 return true
 }
 return false
}

function radio2_onclick() {
for(i=0;i<2;i++)
{
 if (a.radio2[i].checked)
   return true;
}
 return false  
}

function submit1_onclick() {
if (radio1_onclick() && radio2_onclick())
  a.submit()
 else
  alert("false")
   
}

//-->
</SCRIPT>
</HEAD>
<BODY>
<form name="a" action="Reservation.asp" method=post  >
<INPUT type="radio" id=radio1 name=radio1 value="" >abc
<INPUT type="radio" id=radio1 name=radio1 value="">efg
<INPUT type="radio" id=radio2 name=radio2 value="" >123
<INPUT type="radio" id=radio2 name=radio2 value="">234
<INPUT type="button" value="Submit" id=submit1 name=submit1 LANGUAGE=javascript onclick="return submit1_onclick()">
</form>
</BODY>
</HTML>

0
 

Author Comment

by:JavaBoy060299
ID: 2810205
Hi indu_ma,

So, I have to create 10 function, if I have 10 radio groups ?
what about if I have more than 10 groups ?
Is there any other better solution ?

0
 
LVL 3

Expert Comment

by:bark10
ID: 2810932
I think this is more of what you are looking for.  This script checks for checked boxes in 10 questions.  (To change the number of questions checked, change:
for (var i=1; i<11; i++)  to
for (var i=1; i< (#questions+1) ; i++)
)

The script:

<script language="javascript">
function ckradio()
{
      ok=0;
      for (var i=1; i<11; i++)
            {
            for (var j=0; j<eval("a.q"+i+".length"); j++)
                  if (eval("a.q"+i+"[j].checked"))
                        {
                        ok=1;
                        break
                        }
            if (ok==0)
                  {
                  alert("Please make selection for Q"+i)
                  return false;
                  }
            else
                  ok=0;
            }
return true;
}

function validate()
{
      if (ckradio())
            a.submit()
}
            
</script>



The calling button:

<input type="button" value="Submit" onClick="return validate()">


PS.  for this to work, radio buttons must be numbered as q#, if you have a different form, I could change the script for you.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:indu_ma
ID: 2813092
indu_ma changed the proposed answer to a comment
0
 
LVL 2

Accepted Solution

by:
indu_ma earned 50 total points
ID: 2813101
Iterate through each element check whether it a radio button and then check whether any one choice is checked
I have assumed that i have two questions
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">


<SCRIPT ID=clientEventHandlersJS LANGUAGE=javascript>
<!--
function submit1_onclick() {
var f,c
f=0  
for (i=0;i<a.elements.length;i+=4)
{
if (a.elements[i].type=="radio")
{
if ((a.elements[i].checked)|| (a.elements[i+1].checked)|| (a.elements[i+2].checked) || (a.elements[i+3].checked))
  f++;
 else
 {
    c=f+1;
   alert("Question No " + c +" Not Answered")    
 }
}
}
 
 if (f==2)  //2 implies that we  
 a.submit();  have two questions

//-->
</SCRIPT>
</HEAD>
<BODY>
<form name="a" action="Reservation.asp" method=post>
Question No:1
<INPUT type="radio" id=radio1 name=radio1 value="1" >abc
<INPUT type="radio" id=radio1 name=radio1 value="1">efg
<INPUT type="radio" id=radio1 name=radio1 value="1">pqr
<INPUT type="radio" id=radio1 name=radio1 value="1">del
<br>
Question No:2
<INPUT type="radio" id=radio2 name=radio2 value="2" >123
<INPUT type="radio" id=radio2 name=radio2 value="2">234
<INPUT type="radio" id=radio2 name=radio2 value="2">567
<INPUT type="radio" id=radio2 name=radio2 value="2">789
<INPUT type="button" value="Submit" id=submit1 name=submit1 LANGUAGE=javascript onclick="return submit1_onclick()">
</form>
</BODY>
</HTML>
0
 
LVL 3

Expert Comment

by:bark10
ID: 2813877
Kind of what I proposed, huh indu_ma?
0
 
LVL 3

Expert Comment

by:bark10
ID: 2814133
You should be able to paste mine directly into your page.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now