Skip to main content
Participant
July 6, 2020
Question

How to add units to a field in a form

  • July 6, 2020
  • 4 replies
  • 3014 views

Hello,

 

I am preparing a form, where the user needs to enter a length, for example.

I would like to set it up where they would just enter '8000', but the field would say '8,000.00 feet'.

 

A few methods I have tried using Custom Format Scripts:

event.value = event.value + " feet"                 - this is good, but I can't do formatting on the number

AFNumber_Format(1, 0, 0, 0, 0, false)          - formats the number, but I can't add the text at the end.

 

Is there a way to combine the two?

This topic has been closed for replies.

4 replies

Thom Parker
Community Expert
Community Expert
July 6, 2020

Use this code in the custom format event.

This is the correct way to do it.

if((event.value == "") || (event.value == 0))
    event.value = "";
else
    event.value = util.printf("%,0.2f feet",event.value);

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
July 6, 2020

Reverse the order of those two lines...

ls_rbls
Community Expert
Community Expert
July 6, 2020

Which one the two lines of the custom format script?

try67
Community Expert
Community Expert
July 6, 2020

Of the original script posted above.

ls_rbls
Community Expert
Community Expert
July 6, 2020

++edited reply-- fixed a minor typo

 

Place this line of code as the Custom Fomat Script of that field:

 

 

if (event.value !=="") event.value = event.target.value + " feet";
else  event.target.value ="";

 

 

And move this other line to the Custom Calculation Script section  by itself :

 

 

AFNumber_Format(1, 0, 0, 0, 0, false) 

 

 

Old_Salt
Participating Frequently
July 6, 2020

Nice!. Much more elegant than my solution.

ls_rbls
Community Expert
Community Expert
July 6, 2020

Thanks, but since I've been following you in the forums you've been emphasizing in using event.target.value. So kudos to you on that.

 

I was going to add that if the user wants to perform calculations with that field and use it with other calculated fields then definitely your script has to be incorporated.

 

This is just to display that value with "feet" added to it.   

 

Old_Salt
Participating Frequently
July 6, 2020

Try this code as an "onBlur" action script for the field. This script does what you want but prevents anything but a number being added.

//OnBlur action
if ((event.target.value != null && event.target.value != "") && Number(event.target.value)) {
	var num = util.printf("%,0.2f", Number(this.getField(event.target.name).value).toFixed(2));
	event.target.value = String(num) + " feet";
} else {
	event.target.value = "";
}

One thing you don't say is what do want the fields actions to be if there is already a number and you want to change the number. The best way is to empty out the field using an onFocus action script with this code then enter the new number.

//OnFocus action
event.target.value = "";

I am using event.target.value and I do understand the differences between event.value and event.target.value but I have never been able to successfully use event.value. For whatever reason, using event.target.value has always worked for me.

 

Perhaps someone has a better way to do what you want. I'm always open to learning better ways to solve a problem.

 

 

 

Thom Parker
Community Expert
Community Expert
July 6, 2020

Very bad idea, DO NOT USE ONBLUR TO SET FORMAT.

 

 

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