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,
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.");}
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.");}
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
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.");}
Copy link to clipboard
Copied
It is working perfectly as I wanted.
Thank you so much for your help.
Kassandra
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.
Copy link to clipboard
Copied
If you had time to read OP post you would see that she asked for that specific input.
Copy link to clipboard
Copied
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?
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:
See also these links:
https://answers.acrobatusers.com/how-make-fractions-form-q238420.aspx