Solved

Radio Buttons

Posted on 2004-04-19
6
426 Views
Last Modified: 2006-11-17
Ok, im trying to validate radio buttons, there are two questions, both need to be answers. At the moment the submit button is button type, if i change it to submit the form is submitted even if the alert boxes apear. i have the following:

<script language="JavaScript">
function Validate(frm, btnName)
{
      var btn = frm[btnName]
      var valid
      
      for (var x = 0;x < btn.length; x++)
      {
            valid = btn[x].checked
            if (valid) {break}
      }
      if(!valid)
      {
            alert("Please select an answer.")
      }
      
      var btn = frm.question2
      for (var x = 0;x < btn.length; x++)
      {
            valid = btn[x].checked
            if (valid) {break}
      }
      if(!valid)
      {
            alert("Please select an answer.")
      }
}
</script>
<body>
<form action="confirm.htm">
      <input type="radio" name="question1" id="1b1" value="1">Always</input>
      <input type="radio" name="question1" id="1b2" value="2">Often</input>
      <input type="radio" name="question1" id="1b3" value="3">Sometimes</input>
      <input type="radio" name="question1" id="1b4" value="4">Rarely</input>
      <input type="radio" name="question1" id="1b5" value="5">Never</input>

      <div>
      <input type="radio" name="question2" id="11b1" value="1">Always</input>
      <input type="radio" name="question2" id="11b2" value="2">Often</input>
      <input type="radio" name="question2" id="1b3" value="3">Sometimes</input>
      <input type="radio" name="question2" id="11b4" value="4">Rarely</input>
      <input type="radio" name="question2" id="11b5" value="5">Never</input>
      
            <input type="button" value="Submit Answer" onClick="Validate(this.form, 'question1')"/>
      </div>
</form>
0
Comment
Question by:mattdye
[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
  • 4
  • 2
6 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 10857547
This might be a way to solve this. First, instead of onClick, do onSubmit.

Make it onSubmit="return Validate(this.form, 'question1')"/>

Then, whenever you need an alert, below the alert, put this line:

return false;

--Zyloch
0
 

Author Comment

by:mattdye
ID: 10857683
ok, it now looks like this and it doesnt validate anything


<script language="JavaScript">
function Validate(frm, btnName)
{
      var btn = frm[btnName]
      var valid
      
      for (var x = 0;x < btn.length; x++)
      {
            valid = btn[x].checked
            if (valid) {break}
      }
      if(!valid)
      {
            alert("Please select an answer.")
            return false;
      }
      
      var btn = frm.question2
      for (var x = 0;x < btn.length; x++)
      {
            valid = btn[x].checked
            if (valid) {break}
      }
      if(!valid)
      {
            alert("Please select an answer.")
            return false;
      }
      
}
</script>
<body>
<form action="confirm.htm">
      <input type="radio" name="question1" id="1b1" value="1">Always</input>
      <input type="radio" name="question1" id="1b2" value="2">Often</input>
      <input type="radio" name="question1" id="1b3" value="3">Sometimes</input>
      <input type="radio" name="question1" id="1b4" value="4">Rarely</input>
      <input type="radio" name="question1" id="1b5" value="5">Never</input>

      <div>
      <input type="radio" name="question2" id="11b1" value="1">Always</input>
      <input type="radio" name="question2" id="11b2" value="2">Often</input>
      <input type="radio" name="question2" id="1b3" value="3">Sometimes</input>
      <input type="radio" name="question2" id="11b4" value="4">Rarely</input>
      <input type="radio" name="question2" id="11b5" value="5">Never</input>
      
            <input type="submit" value="Submit Answer" onSubmit="return Validate(this.form, 'question1')"/>
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 10857715
Let me check.
0
Industry Leaders: 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 36

Expert Comment

by:Zyloch
ID: 10857737
<script language="JavaScript">
function Validate(frm, btnName)
{
     var btn = frm[btnName]
     var valid
     
     for (var x = 0;x < btn.length; x++)
     {
          valid = btn[x].checked
          if (valid) {return true;}
     }
     if(!valid)
     {
          alert("Please select an answer.")
          return false;
     }
     
     var btn = frm.question2
     for (var x = 0;x < btn.length; x++)
     {
          valid = btn[x].checked
          if (valid) {return true;}
     }
     if(!valid)
     {
          alert("Please select an answer.")
          return false;
     }
     
}
</script>
<body>
<form  onSubmit="return Validate(this,'question1')" action="confirm.htm">
     <input type="radio" name="question1" id="1b1" value="1">Always</input>
     <input type="radio" name="question1" id="1b2" value="2">Often</input>
     <input type="radio" name="question1" id="1b3" value="3">Sometimes</input>
     <input type="radio" name="question1" id="1b4" value="4">Rarely</input>
     <input type="radio" name="question1" id="1b5" value="5">Never</input>

     <div>
     <input type="radio" name="question2" id="11b1" value="1">Always</input>
     <input type="radio" name="question2" id="11b2" value="2">Often</input>
     <input type="radio" name="question2" id="1b3" value="3">Sometimes</input>
     <input type="radio" name="question2" id="11b4" value="4">Rarely</input>
     <input type="radio" name="question2" id="11b5" value="5">Never</input>
     
          <input type="submit" value="Submit Answer"/>

Now it should work. The main problem was the onSubmit was in the wrong location AND you don't access the form by doing this.form. It's just this.

--Zyloch
0
 

Author Comment

by:mattdye
ID: 10861467
ok thats cool, but it doesnt validate the questio2 set, ps, thanks for the help
0
 
LVL 36

Accepted Solution

by:
Zyloch earned 70 total points
ID: 10862680
Oh! Sorry, I was at school. I think I know the problem. The script section should be:

<script language="JavaScript">
function Validate(frm, btnName)
{
     var btn = frm[btnName]
     var valid
     
     for (var x = 0;x < btn.length; x++)
     {
          valid = btn[x].checked
          if (valid) {break;}
     }
     if(!valid)
     {
          alert("Please select an answer.")
          return false;
     }
     
     var btn = frm.question2
     for (var x = 0;x < btn.length; x++)
     {
          valid = btn[x].checked
          if (valid) {return true;}
     }
     if(!valid)
     {
          alert("Please select an answer.")
          return false;
     }
     
}
</script>

This is because the first return true; in the previous script exited the function. I tested this and it works fine.

--Zyloch
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

740 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