Skip to main content
10sGerar
Participant
September 7, 2024
Question

Dealing with empty form fields in email

  • September 7, 2024
  • 4 replies
  • 640 views

I'm a total beginner and I have "elementary shcool" level of Javascript knowledge.

With google's help, I was able to get a button, that sends an email with custom subject and body using data from the fields in the form, working.

Where I have a problem is when checking if a field is empty.  The form has 3 field names.

First one is always filled out so I don't need to check. 2nd and 3rd ones however I need to check if they are empty so I can include them in the email or not.

When I use the code below the script doesn't even run when I press the button which leads me to think that there's an error and that's why it doen't run.

Besides of "valueAsString" I have also tried just "value" but I get the same result.

I also tried the === instead of the ==

Any ideas what I'm doing wrong?

 

The code I use is: 

var RR2Name = this.getField("RR1Name").valueAsString;
var RR2Name = this.getField("RR2Name").valueAsString;
var emailSubject;

if (RR2Name =="") {
emailSubject = RR1Name + " form"
} else {
emailSubject = RR1Name + " and " + RR2 + " form"
}

 

This topic has been closed for replies.

4 replies

try67
Community Expert
Community Expert
September 7, 2024

You need to post the full code or share the file. This is not enough information to know what's wrong.

Thom Parker
Community Expert
Community Expert
September 7, 2024

If a field value is a single space, Is is empty?  If so, then your code won't detect it. 

Maybe you should expand the empty field test so that misc white space is part of the empty test. 

 

 

var RR1Name = this.getField("RR1Name").valueAsString;
var RR2Name = this.getField("RR2Name").valueAsString;
var emailSubject;
// Create regular expression for detecting text 
// composed solely of 0 or more white spaces, i.e., blanks.
var rgEmpty = /^\s*$/;

if (rgEmpty.test(RR2Name)) {
emailSubject = RR1Name + " form"
} else {
emailSubject = RR1Name + " and " + RR2 + " form"
}

 

 

You might also be interested in this article:

https://acrobatusers.com/tutorials/dynamically-setting-submit-e-mail-address/

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
September 7, 2024

Check the JavaScript console for errors.

10sGerar
10sGerarAuthor
Participant
September 7, 2024

I don't see any errors. I'll post the whole code when I get back home. Maybe it's something else. 

Nesa Nurani
Community Expert
Community Expert
September 7, 2024

You named both variables 'RR2Name' and in the script you use RR1Name, so change one to RR1Name.

There may be other issue, it is best to post full script if you still have issues.

10sGerar
10sGerarAuthor
Participant
September 7, 2024

My appologies Nesa. They are in fact RR1Name and RR2Name. When I was copying from my work computer I mistyped and I can't find a way to correct my post.