Skip to main content
Inspiring
Nov 2, 2020 4:44 PM UTC
Answered

Formating fields for fractions

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 is closed to new replies. Start a new post to keep the conversation going.
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
Nov 2, 2020 7:27 PM UTC

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

 

8 replies

ls_rbls
Community Expert
Community Expert
Nov 2, 2020 7:19 PM UTC

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/

Translate
Technical Generalist
Nesa Nurani
Community Expert
Community Expert
Nov 2, 2020 6:57 PM UTC

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

Translate
Inspiring
Nov 2, 2020 7:08 PM UTC

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

 

Translate
Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
Nov 2, 2020 7:27 PM UTC

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

 

Translate