Skip to main content
Participant
August 3, 2020
Question

Looking for Javascript to run calculation after selecting radio button

  • August 3, 2020
  • 2 replies
  • 1177 views

Hello!

Just learning about the capabilities of javascript in Acrobat, so please bear with my terminology -

 

What I am looking to do is when a user selects the their preference for "Size" radio button with Small, Medium, or Large as the options the numerical value of a different text field is limited to certain increments.

 

Example:

If used selects Large then the value of the text field can only be input as increments of 50 (50,100,150,200,250...)

 

Is this even possible in Javascript? I know it would be easier to solve in HTML, but I am limited to Javascript at the moment.

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
August 4, 2020

You can use this code as the custom Validation script of the text field to achieve it:

 

var v = Number(event.value);
var size = this.getField("Size").valueAsString;
if (size=="Large" && v%50!=0) {
	app.alert("You need to enter a value that's a multiple of 50.",1);
}

 

Note that it will not work if the user first enters a value into the text field and then changes their selection in the Size radio-button group, though.

Participant
August 4, 2020

Thank you thank you! This works perfect.

ls_rbls
Community Expert
Community Expert
August 3, 2020

You can achieve this very easily with JavaScript as well, and there's always room to get the same result with different mthods.

 

This is how I would do it (even though I acknowledge that there may be better ways. I also consider that you get better results with checkboxes):

 

Renane the three radio buttons objects with the same field name. For my example I will use "Group". Then assign to each radio button a different export value ("Small", "Medium", "Large", for example (this would make it easier).

 

An in the text field where you want to display the value with increments of 50 use a custom calculation script.

 

Put this code as custom calculation script of your text field:

 

var g = this.getField("Group");
var f = event.target;

if (g.value !="Large")  event.value = f;
else if (g.value ="Large") event.value = f.value+50;

 

This is what worked for me.   Any input in the text field would add up 50 automatically to whatever the numerical value is input in that text field.

 

You should also open the field properties, and go to the Format tab. Select "Number" from the built-in list of custom formats(no currency or any other symbol of course, and ) decimal spaces).  This will ensure that the users only enter a number, not strings of text.

Participant
August 3, 2020

Thanks!

 

I inserted that custom calculation script and it accomplished adding 50 to the number inputted.

but I may not of described quite everything I was after. 

 

I want the script to only allow the user to input amounts at increments of 50. 

 

For Example: 

If user inputs 40 into the text field, they'll get an error message until they input a number in an increment of 50 (50,100,150,200,250....)