Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

Way to invoke pattern validation in script

Avatar

Level 2

Hi,

Is there a way to invoke pattern validation in script? or check if the field passes pattern validation?

The reasoning behind it is to avoid writing validation script which essentially does the same thing as parrtern validation.

Thanks.

5 Replies

Avatar

Level 10

Hi,

If you are using validation patterns and the no pattern matched the entered data then the formattedValue will equal the rawValue.  If the pattern was matched then the rawValue will be the canonical format, which for a date is YYYY-MM-DD, time is HH[MM[SS[.FFF][z]]], ...

So if your field is a text field you probably still have a problem. So for things like a phone number or email I haven't been using a pattern and validate the field with a regex in the exit event.

Regards

Bruce

Avatar

Former Community Member

Hello drcaesar-21qe0k!

Any form node (Text Field, Drop-down List, etc) has a method you can invoke called "execValidate".  This method invokes both the pattern and the script validation for the given node - in that order.  If you had a form with a text field entitled txtTest for which you would like to run the pattern validation on, you would simply type:

txtTest.execValidate();

Please let me know if you have any questions!

Josh Boyle

jboyle@cardinalsolutions.com

Cardinal Solutions Group

Avatar

Level 2

Hi Josh,

Thanks for your reply.

I know execValidate() invokes validation. But is there a way to know if the field passed the validation test after the call?

My question was if there's a way to know it.

Thanks again.

Avatar

Level 2

I know this is a little late but was this question ever answered?

james

Avatar

Former Community Member

As a follow-up on this conversation, I don't know of a way to see if a particular field passed a validation pattern after running execValidate().  As a best practice, I would highly recommend regular expressions for validations.  I don't personally find the validation patterns built into the fields to be terribly useful considering how much more power you have at your disposal with Javascript.  One of the first things I do for every client I'm at when developing a form is build out (or copy from a previous form) a reusable script fragment that contains all sorts of validations.  All of the validations use regular expressions.  Then, on the exit event of whatever fields I need to validate, I write something along these lines:

if(!Scripts.isValidDate(this.rawValue))

{

     this.mandatory = "";

     xfa.host.messageBox("The entered date is invalid.  Manually-entered valid date formats are as follows:\n\n- -YYYY-MM-DD\n- YYYY-M-D\n- YYYY-MM-D\n- YYYY-M-DD");

     this.rawValue = "";

     this.mandatory = "error";

}

The lines which set "mandatory" to "" and "error" respectively are there as a workaround to prevent the PDF from displaying a second dialog box complaining about a required field being empty.  This is caused by line 3 within the if block where we clear out the rawValue.  If the field you're validating is not a required field, you can omit those two lines.