Skip to main content
stepheng54012748
Known Participant
November 13, 2024
Answered

javascript a value from another field to another field

  • November 13, 2024
  • 1 reply
  • 1497 views

Hello,

 

I have a field that is a number that results (when entered) 999-99-9999.  This is field "1a".  I'm trying to get this result to show up in another field "a14a7" without the "-" between the numbers so it all comes together as 999999999.  This is the script i wrote, which is pulling the number entered but my result still has the hyphens.  Can anyone help me with this?

 

if(this.getField("1a").value)
{event.value=this.getField("1a").valueAsString}
else
{event.value=this.getField("").valueAsString}

 

This topic has been closed for replies.
Correct answer Thom Parker

Still not working.  When enter the number in the field the first time, it seems to be working.  When i delete it the number just stays.  Here is the script i put in "a14a7":

 

if(this.getField("1a").value)
event.value=this.getField("1a").valueAsString.replace(/\-/g,"");
else
event.value=this.getField("").valueAsString;

 

and here is validation i put in:

 

this.getField("a14a7").value = event.value.toString().replace(/\-/g,"");


Delete the script in "a14a7".  

Only the validation script in "1a" is needed. This validation script moves the data in "1a" into "a14a7".

 

 

1 reply

Thom Parker
Community Expert
Community Expert
November 13, 2024

Try this

 

if(this.getField("1a").value)
    event.value=this.getField("1a").valueAsString.replace(/\-/g,"");
else
    event.value=this.getField("").valueAsString;

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
stepheng54012748
Known Participant
November 13, 2024

PERFECT!!!  Thank you!

Thom Parker
Community Expert
Community Expert
November 13, 2024

If you have a lot of calculations, then form performance could be improved by using a Validation script for this action instead of a Calculation script.  Calculation scripts are run every time any field on the for is changed, whereas the Validation is only run when the specific field is changed. This solution works because field "a14a7" only get it's value from a single field. So that value can be pushed directly from the source field. 

 

To do this, delete the calculation on field "a14a7", and then add this script to the Custom Validation script on "1a";

 

if(event.value)
    this.getField("a14a7").value = event.value.toString().replace(/\-/g,"");
else
    this.getField("a14a7").value = "";

 

 

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