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

Formating fields for fractions

Explorer ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

I need a little help here. 

I have a form with many fields. Client will use fractions (ex. 30 1/4) to enter dimensions in some fields (such as width).

I would like to add and AppAlert when one of this field is more than 30. Since I have no specific format on these fields, they appear as text. I can't do a math validation on a text.

Is there any way to create an AppAlert by converting the amount in the specific fields or simply add a particular formating on these ? 

I am using Acrobat DC Ppro.

Thanks, 

TOPICS
Acrobat SDK and JavaScript

Views

964

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

correct answers 1 Correct answer

Community Expert , Nov 02, 2020 Nov 02, 2020

Try this:

if(event.value > 30){
app.alert("Number need to be less then 30.");}
function toDecimal(_x) {
var parts = _x.split(" ");
var decParts = parts[1].split("/");
return parseInt(parts[0], 10) +
(parseInt(decParts[0], 10) / parseInt(decParts[1], 10));}
var x = (toDecimal(event.value));
if(x > 30){
app.alert("Number need to be less then 30.");}

 

Votes

Translate

Translate
Community Expert ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

If you will enter fractions in this format like in your example( 33 1/4 ) you can try something like this as "custom calculation script" of input field:

function toDecimal(_x) {
var parts = _x.split(" ");
var decParts = parts[1].split("/");
return parseInt(parts[0], 10) +
(parseInt(decParts[0], 10) / parseInt(decParts[1], 10));}
var x = (toDecimal(event.value));
if(x > 30){
app.alert("Number need to be less then 30.");}

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
Explorer ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

Thanks a lot. It is working when entering fractions number (eg. 30 1/4). 

But it could be possible to have an integer value in this field such as 30. I'd like to have the alert when entering any number more than 30. Is it possible to add something to your script to make it work for both cases ?

Numbers could be written like this :

30

30 1/4

Thanks again,

Kassandra

 

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 ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

Try this:

if(event.value > 30){
app.alert("Number need to be less then 30.");}
function toDecimal(_x) {
var parts = _x.split(" ");
var decParts = parts[1].split("/");
return parseInt(parts[0], 10) +
(parseInt(decParts[0], 10) / parseInt(decParts[1], 10));}
var x = (toDecimal(event.value));
if(x > 30){
app.alert("Number need to be less then 30.");}

 

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
Explorer ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

It is working perfectly as I wanted. 

Thank you so much for your help.

Kassandra 

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 ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

The script above makes several assumtions about the input value that may not be true. It will only work if the user enters data is a specific format. To properly handle entering fractional values, you need general purpose parsing code. ls_rbs points to another thread below where Gkaiseril provides a better solution, and an example file you can download from DropBox. I would strongly suggest using this solution.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Enthusiast ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

If you had time to read OP post you would see that she asked for that specific input.

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 ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

LATEST

No, if you had read the thread you'd see that she specifically states the input could be a single interger value. But this isn't the only variation that could cause errors, extra spaces will also cause the script to break.  And even if a specific format were required on entry, how would it be guarenteed, or detected for validation? 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Nov 02, 2020 Nov 02, 2020

Copy link to clipboard

Copied

This is not an easy scripting task, but I was able to find a thread enswered by ACP Gkaiseril that should give you an idea of how to work around what you're asking:

 

https://community.adobe.com/t5/acrobat/how-to-store-fraction-value-in-text-field-and-then-convert-it...

 

See also these links:

 

https://answers.acrobatusers.com/how-make-fractions-form-q238420.aspx

 

https://mathjs.org/

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