use calculate to pre-fill a field

New Here ,
Feb 26, 2020

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

73

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

use calculate to pre-fill a field

New Here ,
Feb 26, 2020

Copy link to clipboard

Copied

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?

TOPICS
Acrobat SDK and JavaScript, PDF forms

Views

74

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 26, 2020 0
Adobe Community Professional ,
Feb 27, 2020

Copy link to clipboard

Copied

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2020 0
New Here ,
Feb 27, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2020 0
Most Valuable Participant ,
Feb 27, 2020

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2020 0
Adobe Community Professional ,
Feb 27, 2020

Copy link to clipboard

Copied

Use a checkbox to override the calculation.

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 27, 2020 0