Skip to main content
Participant
February 26, 2020
Question

use calculate to pre-fill a field

  • February 26, 2020
  • 3 replies
  • 409 views

Hi

 

I have a form where I want to pre-fill the e-mail field based on firstname and lastname fields. I have this script in the email field:

var fornavn = this.getField("Fornavn").valueAsString;
var etternavn = this.getField("Etternavn").valueAsString;
fornavn = fornavn.replace(" ","");
etternavn = etternavn.replace(" ","");
event.value = fornavn + "." + etternavn + "@company.com";

 

But how can I allow the user of the form to override this and enter their own email address if they don't want to have the one suggested by the script?

This topic has been closed for replies.

3 replies

Thom Parker
Community Expert
Community Expert
February 27, 2020

Use a checkbox to override the calculation.

In a calculation script, "event.rc = false" will block the field from being updated from the scirpt.

 

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

This is a bit complicated to implement. Also, there's the question of when it should update and when it shouldn't.

If you set it up to update the value when either of the name fields are edited, it could happen that the user entered an email address and then changed one of those fields and it overwrites the value they already entered, which might not be desired.

Because of that I would use a button field to generate the automatic email address when clicked, instead of a calcuation script. That way the user can control when it happens.

BarlaeDC
Community Expert
Community Expert
February 27, 2020

Hi,

 

You could just check if the field has anything in it and if it does then don't run your script, that way if it is empty,  you can populate it, and if the user overwrites it it will stay.

 

// something like.

if (event.value == "")

{ // populate; }

 

Regards

 

Malcolm

 

kmlundeAuthor
Participant
February 27, 2020

I'm not sure if that would help as the field is populated already when you come to it. If you enter something in the first field (fornavn/firstname) then the event automaticly triggers the email field to change when you exit the firstname field.