Solved

check if 3 radio buttons exists?

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
make control visible based off other control value 7 45
javascript and jquery question 4 46
HTML editor custom button 3 88
Multiple of Image Swaps 5 41
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…
Suggested Courses

739 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