Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Radio Buttons

Posted on 2004-04-19
6
Medium Priority
?
431 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…
Suggested Courses

971 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