How do I set up a form to create a part number based on radio button selections

I want to create a form that creates a part number, in its own field(s), based on radio button selections.

For example, if there are four segments to the part number, when a user makes a radio button selection for segment 1, that portion of the part number appears. Then, radio button selection for segment 2 is made, that portion of the part number appears, and so on.


Ross E


3 Answers

Voted Best Answer

So you mean that you have several radio-button groups, and when a selection is made in each of them you want to add that selection to the part number text field?
You can do it by assigning the appropriate segment values to each radio-button and then use something like this as the custom calculation code for your text field:

var newValue = "";
for (var i=1; i<=4; i++) {
newValue += this.getField("Segment "+i).value=="Off" ? "" : this.getField("Segment "+i).value;
}
event.value = newValue;

This assumes, of course, that your radio button groups are called "Segment 1", "Segment 2", etc.


By Gilad D (try67)   

The approach above may need to be modified, depending on how "no selection" is treated in the part number. If a part number MUST have all segments selected, you would first verify if none of the checkbox groups' value is "Off". If one or more values is "Off" (meaning that no selection has been made), you may either not assemble the number at all, or display a warning that the number is not (yet) valid. If you have a placeholder for a missing segment, you would add this placeholder instead of the empty string. Or instead of testing whether the segment's value is "Off", you could do a simple replace, using this piece of code:

myValue = myValue.replace(/Off/,"replacestring") ;


An additional advice: if you have a complex form with various calculations, it may be worthwile to assign the calculation of the part number to the MouseUp event of the radio buttons instead of the Calculate event of the part number field; this would make your form more responsive.


Max Wyss   

Thank you for the help. I don't know javascript, but one thing I did was the following:

- Create a separate text box for each segment (instead of one text box for the whoel string)
- Use the following javascript as the custom calculation script (appended from the original code):

var newValue = this.getField("Segment 1").value=="Off" ? "" : this.getField("Segment 1").value;event.value = newValue;

In this case, you have to indicate "Segment 1", "Segment 2", etc. Probably not the cleanest code? But it is working.


Ross E   


Please specify a reason: