These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Required Field Check in Email Button

30011754
Registered: Jan 6 2010
Posts: 13
Answered

Newbie to JavaScripting and need some guidance.. Not sure if im setting this correct but I have (2) JavaScripts on "Mouse Up" trigger in a email button. First java script runs the validation of the required fields and the second attaches the PDF in a email. I have everything working fine except that when there is a missing required field it prompts the user but still sends the email. I want the script to stop and not let the user send the email taking them back to the form till all required fields are met. Using Adobe Pro 9..
 
//JavaScript #1 (Validates the required fields)
 
var requiredFields = new Array();
for (var i = 0; i < this.numFields; i++)
{
var fName = this.getNthFieldName(i);
var f = this.getField(fName);
if (f.type!="button" && this.getField(fName).required && (this.getField(fName).value == null || this.getField(fName).value == ''))
{
requiredFields[requiredFields.length] = fName;}}
var error = "Please complete the following fields: \n\n";
for (j=0; j < requiredFields.length; j++)
{
if (requiredFields[j].value == null)
{
error = error + requiredFields[j] + '\n';
var f2 = this.getField(requiredFields[j]);
f2.setFocus();
}
  
}
  
if (requiredFields.length > 0)
app.alert(error);
 
//JavaScript #2 (this attaches the pdf to the email)
 
{
// This is the form return email, It’s hardcoded
// so that the form is always returned to the same address
// Change address on your form
var cToAddr = "Miami DDA Services HBUS";
 
// Set the subject line
var cSubLine = "FX DDA AND/OR CALL ACCOUNTS";

 
// Send the form data as an PDF attachment on an email
this.mailDoc({bUI:true, cTo: cToAddr,cSubject: cSubLine});
}

My Product Information:
Acrobat Pro 9.0, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
Change the last part to something like:

if (requiredFields.length > 0) {
app.alert(error);
} else {

// JavaScript #2 goes here

}

Also, code like this:

(this.getField(fName).value == null || this.getField(fName).value == '')

can be simplified to:

this.getField(fName).value == '')

Since a field value will never be equal to the special JavaScript value of null. It is misleading to test for it. A blank text field value will be equal to the empty string (""), so that's the value you should test for.
30011754
Registered: Jan 6 2010
Posts: 13
Thanks George!! Its working perfect now with your change request.. Everything is in one script now which makes sense..
30011754
Registered: Jan 6 2010
Posts: 13
George, I just noticed that "Text" fields are being validated but if I have a radio button or a dropdown field required these are overlooked in the validation. Anyway I can include these in the validation?
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Since those types of fields always have a non-blank value, setting the required property doesn't have an effect unless you use a script to check each one. So your script should check each field and see if its value is valid, as opposed to just checking if it's empty. Unchecked check boxes will have a value of "Off" and combo boxes will have a value equal to the item value or export value if present.
dpelchat
Registered: Sep 2 2011
Posts: 3
Hi, sorry to wake up the dead, but ... I copied this script for my own use, but it seems to validate all fields from a form and not just the ones that are required. Am I doing something wrong, or is there something I must do differently ? Thanks!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Which code, exactly, are you using?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

dpelchat
Registered: Sep 2 2011
Posts: 3
same as the original poster + mods suggested above.

It's basically working, but forcing me to enter a value for every field I have in the form, not just the ones that are required.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
As far as I can see, that code only verifies fields that are defined as required.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
If you wish, send me the file and I'll have a look at it (my email address is in my signature).

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

dpelchat
Registered: Sep 2 2011
Posts: 3
Ok I see what's going on ... I probably cut & paste the "code simplification" part wrong. Using the original code seems to work just fine !Thanks for your help. This is my first form and I have basically no expertise in these things. Thankfully, this website seems to have a lot of answers !