Solved

check if 3 radio buttons exists?

Posted on 2004-10-12
4
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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

636 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