Skip to main content
Inspiring
November 2, 2020
Answered

Formating fields for fractions

  • November 2, 2020
  • 2 replies
  • 1611 views

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, 

This topic has been closed for replies.
Correct answer Nesa Nurani

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.");}

 

2 replies

ls_rbls
Community Expert
Community Expert
November 2, 2020

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-to-decimal/td-p/8816450?page=1

 

See also these links:

 

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

 

https://mathjs.org/

Nesa Nurani
Community Expert
Community Expert
November 2, 2020

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.");}

Inspiring
November 2, 2020

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

 

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
November 2, 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.");}