[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

validation on radio button

Posted on 2000-05-15
7
Medium Priority
?
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 discusses how to implement server side field validation and display customized error messages to the client.
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…

650 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