Solved

Radio Buttons

Posted on 2004-04-19
6
424 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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…
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…

860 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