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

Simple javascript validation example please

cam_pdx
Registered: Oct 26 2009
Posts: 12

Hello,

I have a pdf in Acrobat 9 Pro. I've converted it to a web form which is called from, and submits to a cold fusion application for processing. That all works fine.

Now, I'm trying to put some client-side validation in place. I double click the text input field (e.g. email), click on the Validate tab, and under "Run custom validation script", I enter something as simply as:

alert("test");

Then I try submitting the form with input in that field and without input. The custom Validation script never fires the pop-up.

Can anyone point me to a simple example for making this work?

Thank you very much.

My Product Information:
Acrobat Standard 9.2, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Acrobat does not use the Java Language but it uses the EMCA subset of JavaScript and has some modifications to this set of commands and those are documented in the Acrobat JS API reference, a free download from the the Adobe's Developer's section site for Acrobat.

if (event.value >= 25) {event.rc = false;app.alert({cMsg: "Error! Try again!",cTitle: "Acme Testing Service",nIcon: 0,nType: 1});} // end if validation test

The Acrobat documentation will also explain about the 'rc', return code, property of the event object.

George Kaiser

cam_pdx
Registered: Oct 26 2009
Posts: 12
I'm not thrilled about learning an API for this single piece that I need. Does anyone know of a web page with examples I can copy/paste for various validation types? Specifically: email address.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Verifying email addresses can be tricky. The rules to what is a valid email address are quite complicated and there's no built-in API in JavaScript that can do such validation. You can do it yourself with a regular expression, though. Have a look here for some more info about this subject:
http://www.regular-expressions.info/email.php

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

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Oddly enough, Acrobat does have a two built-in Email test functions, at least in Acrobat 8 and 9, I haven't check the other versions. There are both unadvertised and undocumented because both are part of other built-in and undocumented functionality.

Just place this code in your the validation script for your email field

event.rc = CBIsValidEmail(event.value);

or

event.rc = eMailValidate(event.value);

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

try67
Expert
Registered: Oct 30 2008
Posts: 2398
You learn something new every day, I guess... Thanks for the tip, Thom.

By the way, if anyone's interested here's the source code for both functions:
function CBIsValidEmail(addr) {var emailRE = /^([a-zA-Z0-9_\-\.\/]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,5}|[0-9]{1,3})(\]?)$/;return addr != undefined && addr != "" && addr.match(emailRE) != null;}

function eMailValidate(emailStr) {var emailPat = /^(.+)@(.+)$/;var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";var validChars = "[^\\s" + specialChars + "]";var quotedUser = "(\"[^\"]*\")";var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;var atom = validChars + "+";var word = "(" + atom + "|" + quotedUser + ")";var userPat = new RegExp("^" + word + "(\\." + word + ")*$");var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");var matchArray = emailStr.match(emailPat);if (matchArray == null) {return false;}var user = matchArray[1];var domain = matchArray[2];if (user.match(userPat) == null) {return false;}var IPArray = domain.match(ipDomainPat);if (IPArray != null) {for (var i = 1; i <= 4; i++) {if (IPArray[i] > 255) {return false;}}return true;}var domainArray = domain.match(domainPat);if (domainArray == null) {return false;}var atomPat = new RegExp(atom, "g");var domArr = domain.match(atomPat);var len = domArr.length;if (domArr[domArr.length - 1].length < 2) {return false;}if (len < 2) {return false;}return true;}

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

cam_pdx
Registered: Oct 26 2009
Posts: 12
Getting closer - thank you!

But, the validation still is not kicking in. I've tested clicking submit with no input, garbage input, and an email address in the email field. Regardless, it just submits to the submit url action I have in place. No warnings or alerts.

I put the validation code you suggested into the email text field under the validation tab and picked the "run custom script" option. Did I miss anything?
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to use a condition test for the 'submit' or 'email' button for a valid email address and if that test is passed then process the email.

A user can ignore the validation warning and any given field validation has no affect on any other field's validation, calculation, action, etc.

George Kaiser

cam_pdx
Registered: Oct 26 2009
Posts: 12
Alright, I think I have this figured out (due to the assistance of all you good folk). And here's the summary for those that need it in the future:

To validate email in a pdf form input in Acrobat 9 Pro, you can do the following:
- open the properties of the email field
- click the validate tab
- click "Run custom validation script"
- click "Edit..."
- and I used the following code:

if (! eMailValidate(event.value)) {
event.rc = false;
app.alert({
cMsg: "Invalid email address - Please try again",
cTitle: "Acme Testing Service",
nIcon: 0,
nType: 1
});
} // end if validation test

...for some reason, Acrobat will still allow the user to click "OK" when the error appears and the pdf form will still submit. The user would have to click "Cancel" instead of "OK" to update their email prior to submitting the pdf form. Sure wish this could be corrected.

Thank you VERY much to gkaiseril, try67 and Thom.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You can achieve that if you submit the form using a script instead of Acrobat's built-in functionality.
You can create a button that checks the validity of the input (using an adjusted version of the code above), and if all is well uses this.submitForm() to submit the form.

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

cam_pdx
Registered: Oct 26 2009
Posts: 12
Ahh - understood. Thanks. I'll look into that.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
What Try67 is saying is that there are 2 different kinds of validation. There's Field level validation and Form level validation. You put your email validate script at the Field Level so that the user can only enter a valid email. Then at the form level, when the user tries to submit, all the form has to do is see if the email field contains any data. If it does contain data it's guarenteed to be valid because of the field level validation script.

There are 2 ways to implement Form Level Validation. The first way is to mark the field as "required". This tells Acrobat that the field has to contain data before it allows the form to submit. The second way is to write a submit script that tests the fields of interest before submitting the data.

Take a look at this artcle, the example file covers exactly this issue:
http://www.acrobatusers.com/tutorials/dynamically-setting-submit-e-mail-address

On another note, in your "alert" box code do not include the nIcon and nType inputs. They are unnecessary and the "nType" input is causing the box to display both the "Ok" and "Cancel" buttons. You only need the "Ok" Button.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

cam_pdx
Registered: Oct 26 2009
Posts: 12
Very good to know. Thank you for clearing that up, and the fix for the email validation. I'll read that article. Thank you.

One side note: the user is prevented from deleting what they've entered into a text input which has validation applied. You are only allowed to replace with another valid entry (or reload the form). A form reset button has the same effect (can't remove the input). I get that this is the field-level validation you mentioned, and while it bothers me a little, it fulfills the need for preventing invalid input, so I appreciate it.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Not true, it's all in how you write the script. Replace your email test with this.

if (!eMailValidate(event.value) && !(/^\s*$/.test(event.value)) {Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script