I have a set of 5 radio buttons in a form that are all within the same radio button list (as far as I can tell!) - they look like this in the Hierarchy tab:
rblApprQ1
- rblApprQ1_1
- rblApprQ1_2
- rblApprQ1_3
- rblApprQ1_4
- rblApprQ1_5
I've got a bit of validation on my submit button to check one of the boxes has been checked, as follows:
var
rblApprQ1 = xfa.resolveNode("mainForm.rblApprQ1");
This gets passed into another script object:
function
checkRadioButtons(field)
{
rbChosen
= '';
len
= field.nodes.length;
for (i = 0; i <len; i++)
{
if (field.nodes.item(i).rawValue != null)
{rbChosen
= field.nodes.item(i).rawValue;}
}
if (rbChosen == '')
{
return false;}
else
{
return true;}
}
The problem I have is, if no radio button has been checked, the value of len in the checkRadioButtons function is 5 - I'm presuming this is the 5 separate radio buttons in my radio button list? Yet when any of the buttons are selected, the len value goes up to 7 thus rendering my function rubbish!
Am I doing something wrong?
Solved! Go to Solution.
Views
Replies
Total Likes
You are getting an extra node after the selection is made. If I look at the className property of the object you will see that each radiobutton has a class
name of field and the extra node is of type calculate. So put an extra check in to make sure that you are testing a node of type "field". Then I woudl move the code that returns the true or false outside of the for loop. Right now you are returning it for each radio button in your list. You only need to return a single value. I also changed the name of the passed object from field to test to make it easier to follow. Here is a view of the code that I created to test it:
checkRadioButtons(RadioButtonList)
function checkRadioButtons(test){
rbChosen = '';
len = test.nodes.length;
//app.alert(len)
for (i = 0; i <len; i++){
rbName = test.nodes.item(i);
//app.alert(rbName.saveXML("pretty"));
if (rbName.className == "field"){
if (test.nodes.item(i).rawValue != null){
rbChosen += test.nodes.item(i).rawValue;
}
}
}
if (rbChosen == ''){
app.alert("No RBs were chosen");
} else {
app.alert("RB selection was made");
}
}
Views
Replies
Total Likes
There is no need for all of that code. If you click on the radioButtonList and then go into the Object Palette/Value Tab and set the Type dropdown to
User Entered - Required the product will not allow you to submit without choosing one of the radio buttons.
Paul
Thanks Paul but I prefer using javascript for validation purposes - yes it's long-winded but it's what I'm used to and I like the extra control over how errors are displayed, etc.
So, is there a way to do this with my code?
Views
Replies
Total Likes
You are getting an extra node after the selection is made. If I look at the className property of the object you will see that each radiobutton has a class
name of field and the extra node is of type calculate. So put an extra check in to make sure that you are testing a node of type "field". Then I woudl move the code that returns the true or false outside of the for loop. Right now you are returning it for each radio button in your list. You only need to return a single value. I also changed the name of the passed object from field to test to make it easier to follow. Here is a view of the code that I created to test it:
checkRadioButtons(RadioButtonList)
function checkRadioButtons(test){
rbChosen = '';
len = test.nodes.length;
//app.alert(len)
for (i = 0; i <len; i++){
rbName = test.nodes.item(i);
//app.alert(rbName.saveXML("pretty"));
if (rbName.className == "field"){
if (test.nodes.item(i).rawValue != null){
rbChosen += test.nodes.item(i).rawValue;
}
}
}
if (rbChosen == ''){
app.alert("No RBs were chosen");
} else {
app.alert("RB selection was made");
}
}
Views
Replies
Total Likes
If you're a drinker then the beers are piling up - if not, then I guess a few glasses of water are in order.
Thanks again for the help,
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies