Solved

check if 3 radio buttons exists?

Posted on 2004-10-12
4
306 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
ID: 12290013
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
ID: 12290019
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
ID: 12290151
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
ID: 12290326
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

773 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