Solved

check if 3 radio buttons exists?

Posted on 2004-10-12
4
304 Views
Last Modified: 2012-08-13
Hi,

I am dynamically displaying some radio buttons on a page.

I am trying to check to see if they exist first or if they are not null (!= null)

Then perform my validation.

Again, so I dynamically display these radio buttons using some server side code.

If radio button 2 and 3 are displayed, then I get an error that says one of the radio buttons is null or not an object.

my goal is to only validate the radio buttons that are displayed in the final result.  It seems my code checks for the existence of all three, but if only 2 are displayed on the page, I get this error.

My code is below:

function valForm{
if ( (theFrm.fieldName[0] != null)||(theFrm.fieldName[1] != null)||(theFrm.fieldName[2] != null) )

if(!theFrm.fieldName[0].checked && !theFrm.fieldName[1].checked && !theFrm.fieldName[2].checked)  {
      alert("You must choose a radio button to continue.")
      return false
    }
}

Any help appreciated..
0
Comment
Question by:Westside2004
  • 2
4 Comments
 
LVL 13

Expert Comment

by:cLFlaVA
Comment Utility
Something like this?

var oneIsChecked = true;

for (var i = 0; i < theFrm.fieldName.length; i++) {
    if (!theFrm.fieldName[i].checked)
        oneIsChecked = false;
}

if (!oneIsChecked) {
    alert("You must choose a radio button to continue.");
    return false;
}
0
 
LVL 13

Accepted Solution

by:
cLFlaVA earned 250 total points
Comment Utility
Whoa.  Backup.

var oneIsChecked = true;

for (var i = 0; i < theFrm.fieldName.length; i++) {
    if (theFrm.fieldName[i].checked)
        oneIsChecked = true;
}

if (!oneIsChecked) {
    alert("You must choose a radio button to continue.");
    return false;
}


Sorry, I got all backwards there for a second.
0
 
LVL 9

Expert Comment

by:riyasjef
Comment Utility
This is my script

var oneIsChecked = true;
if(theFrm.fieldName)
{
if(isNaN(theFrm.fieldName.length))
{
   if(!theFrm.fieldName.checked)
          {
               alert("not checked")
               return false;
           }
}
else
{

for (var i = 0; i < theFrm.fieldName.length; i++) {
    if (theFrm.fieldName[i].checked)
        oneIsChecked = true;
}
}
else
{

alert('No buttons");
return false;
}
if (!oneIsChecked) {
    alert("You must choose a radio button to continue.");
    return false;
}

Riyasjef

0
 
LVL 15

Expert Comment

by:justinbillig
Comment Utility
i would suggest changing your approach from radio buttons to a combo box ( select ), it's far more scaleable, what if you need to add 200 more options?
0

Featured Post

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!

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now