Solved

check if 3 radio buttons exists?

Posted on 2004-10-12
4
307 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

735 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