Solved

Radio Buttons

Posted on 2004-04-19
6
409 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

747 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

8 Experts available now in Live!

Get 1:1 Help Now