• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Looking for Javascript to run calculation after selecting radio button

New Here ,
Aug 03, 2020 Aug 03, 2020

Copy link to clipboard

Copied

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.

TOPICS
How to , PDF forms

Views

652

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2020 Aug 03, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 03, 2020 Aug 03, 2020

Copy link to clipboard

Copied

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....)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2020 Aug 03, 2020

Copy link to clipboard

Copied

Sorry, but this code is incorrect and will not work as described. 

The main question is what should be done with a value that's not a multiple of 50 when "Large" is selected. Should it be rounded? If so, in what way? Or should it be rejected?  Or should it be allowed but an error message appears? Or something else? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 03, 2020 Aug 03, 2020

Copy link to clipboard

Copied

It should be allowed, an error message should appear reminding the user they need to input in multiples of 50. There is already supporting text stating this on the form. 

 

Example:

User enters 375 in text field, they should get an alert that they can only order in multiples of 50.

 

Thanks for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 03, 2020 Aug 03, 2020

Copy link to clipboard

Copied

I only got to this part  "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....)" to cover that with  a validation script :

 

if (event.value < 50 ) {
app.beep(0);
app.alert("Invalid value for field " +" " + event.target.name+"You must type a value in multiples of '50'. For example, valid multiples of 50 always end in '50' or with '00' at the end of that number. Valid examples are 100, 150, 200, 250, etc");
event.rc = false;

{

this.resetForm(["MyTextField"]);

} 
}

 

According to  a source that I am reading, the rule of thumb in simple math is that, any number that is a multiple of 50 will always end in "50" or "00" at the end of that number. Like  500, 650, 700, 1000, 1050, 2000,  to name a few examples.

 

I was trying to figure out how to add a variable of event.target.value+"00" with this idea in mind. But then the user can enter 96 in that field and it will convert  that value to 9600, which is incorrect. I am stuck too at this point. 

 

My brain weighs 500 more pounds than an hour ago, when I thought this was going to be easy peasy.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 04, 2020 Aug 04, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 04, 2020 Aug 04, 2020

Copy link to clipboard

Copied

LATEST

Thank you thank you! This works perfect.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines