Validating with multiple radio button sets - Dynamically generated.

Hi All,

I have dynamically generated (From a loop) sets of radio buttons. I am already validating to ensure at least one from each set is selected. However, we have now added a new option (N/A) to each set. All the options are Numeric 1 – 10, and the new option ‘N/A’ is zero (0). I want to check that all the radio button sets are not set to zero (0). At least one set must have a value greater than zero.
What I want to do is to generate a comma delaminated string with all the radio buttons sets names as follows. (I will do this server side with ASP)

strMyRadSets = RadSet_03, RadSet_08, RadSet_22, RadSet_62, RadSet_78, RadSet_152

I now want to pass this string to my validation script to ensure that the accumulated value of selected items of these set are greater than 0.

Something like this ...

RadCheck = document.getElementById(strMyRadSets’).value;
If RadCheck == 0
{
alert("Error … ");
return false;
}

Thanks in advance
LVL 5
Abiel de GrootDeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

Maybe something like this

strMyRadSets = "RadSet_03, RadSet_08, RadSet_22, RadSet_62, RadSet_78, RadSet_152";
var astrMyRadSets = strMyRadSets.split(","); 
var RadCheck = 0
for (var i in astrMyRadSets ) {
  RadCheck = RadCheck + document.getElementById(astrMyRadSets [i]).value;
}
If RadCheck == 0 

Open in new window

Regards
Julian HansenCommented:
Using JQuery you could do something like this
...
$(':radio:checked').each(function() {
  if ($(this).val() == 0) {
     alert('error');
     return false;
  }
});

Open in new window

Sample here
Abiel de GrootDeveloperAuthor Commented:
@Rgonzo1971

I have this ...

strMyRadSets = 'rad_1','rad_8','rad_4','rad_12','rad_3','rad_5'  << Generated from a data loop

var astrMyRadSets = strMyRadSets.split(","); 
var RadCheck = 0
for (var i in astrMyRadSets ) {
  RadCheck = RadCheck + document.getElementById(astrMyRadSets[i]).value;
}

alert(RadCheck)
return false;

Open in new window


Irrespective of what options within each rad group I select RadCheck always shows as 00.

Thanks for your help

@Julian Hansen
I don't want to use JQuery for this.

A
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rgonzo1971Commented:
Sorry can't help further
Julian HansenCommented:
I don't want to use JQuery for this.
Any particular reason why?
Julian HansenCommented:
Here is the equivalent JavaScript function
function validate()
{
  var checked = document.querySelectorAll('input:checked');
  for(var i = 0; i < checked.length;i++) {
    if (checked[i].value == 0) {
      alert('error:' + checked[i].name);
    }
  }
}

Open in new window

Working sample here
Abiel de GrootDeveloperAuthor Commented:
@Julian Hansen

Hi, I now this error.

Does not except this property of method  >> querySelectorAll
Abiel de GrootDeveloperAuthor Commented:
>> RadCheck = RadCheck + document.getElementById(astrMyRadSets[i]).value;
]

Open in new window

Of course the radiobuttons do not have an ID. & Get ElementsByName() does not work.

A
Abiel de GrootDeveloperAuthor Commented:
This is a total local working version to see what is happening.

// Validation for too many N/A's starts here on line 108.

Many thanks

A
Julian HansenCommented:
Does not except this property of method  >> querySelectorAll
What browser are you using?

This is a total local working version to see what is happening.
???
Abiel de GrootDeveloperAuthor Commented:
IE ...

A

Run the copy I uploaded
Julian HansenCommented:
IE ...
... and the version?
Run the copy I uploaded
Uploaded where exactly?



Any particular reason you don't want to use JQuery?
Julian HansenCommented:
Irrespective of what options within each rad group I select RadCheck always shows as 00.
That's because JavaScript is interpreting the values as strings and concatenating them you have to do this
var astrMyRadSets = strMyRadSets.split(","); 
var RadCheck = 0
for (var i in astrMyRadSets ) {
  RadCheck += parseInt(document.getElementById(astrMyRadSets[i]).value);
}

alert(RadCheck)
return false;

Open in new window

Abiel de GrootDeveloperAuthor Commented:
Sorry, I have had to pop out to attend to something. I will get back to this first thing in the morning. .. Sorry guys.

A
Abiel de GrootDeveloperAuthor Commented:
Abiel de GrootDeveloperAuthor Commented:
Still getting a JS error

No se puede obtener la propiedad 'value' de referencia nula o sin definer

Cannot get the property or 'value' referenced, null or undefined.

A
Julian HansenCommented:
No error this side.

Please can you answer the questions asked.

What version of IE are you using this on?

Please advise why JQuery is not an option?
Abiel de GrootDeveloperAuthor Commented:
Hi Sorry,

I am using IE version 11


I have been asked not to use JQuery on this project, but I do (A lot) on other projects, so I understand the frustration.

A.
Julian HansenCommented:
I am using IE version 11
Works fine on IE for me

I have been asked not to use JQuery on this project
Weird, personally I have moved on to Angular but not going with a JavaScript framework and trying to do it all from scratch seems a bit dated to me.

The querySelectorAll should be compatible with all modern browsers.
http://www.w3schools.com/jsref/met_document_queryselectorall.asp

If you browse  to the sample I posted earlier - does that work for you.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abiel de GrootDeveloperAuthor Commented:
Many thanks ... you are right.
Julian HansenCommented:
You are welcome - good luck with your project.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.