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.

Validation on Exit prevents window closing.

Avatar

Level 1

Hi, all.

I have developed a dynamic PDF that has certain "required" fields.  All validation is done in Javascript when the user tries to Exit the field.  If the data does not match my regular expression, a message appears and focus is thrown back to the invalid field.  It is all working just fine except for this - When I attempt to close the window, save the file, print, or do anything that takes me away from an invalid field, the PDF goes into a loop of messageBox alerts and eventually a big error window.  I would appear that this is because of the fact that the script executes on Exit, and the PDF doesn't really take into consideration that I have essentially left the form.

Here is an example of my code:

----- form1.subForm_main_page01.subForm_upper_fixed.Table3.Row1.patientLast::exit: - (JavaScript, client)

var r = new RegExp();
r.compile("^[a-zA-Z\-]*$", "i");

var lastName = this.rawValue;
if(lastName == null) {
    xfa.host.messageBox("Please enter the person's last name.");
    xfa.host.setFocus(this);
}
else {
    if(r.test(lastName) == false) {
        xfa.host.messageBox("Please correctly enter the person's last name.");
        xfa.host.setFocus(this);
    }
}

Has anyone bumped into this before?  I would like to keep validating on Exit, but I don't want my end users to endure an annoying loop of error messages.

Thanks for your help!

-Kyle

5 Replies

Avatar

Level 10

Try moving your code to Validate event instead of exit event.

And also do the same check at the form level before the form is submitted.

Thanks

Srini

Avatar

Level 1

Thank you, Srini.  I've done some work, but it appears the Validate event doesn't quite work for me.

My workflow is a little different than normal.  First, my client requires that each field be properly validated as the user progreses through the form.  Second, the data is not getting posted or sent anywhere.  The end result is that the user simply prints the form and sends it via fax or mail.  To meet the first requirement, the Exit event works almost perfectly.  The problem with the Validate event is that it can allow the field to be null.  I can't allow null values on the fields that I'm validating.  I need to make it so the user cannot proceed until the current field is filled out and validated.

Thanks again!

Avatar

Former Community Member

Hey Kyle!

The Validate and Calculate events are both responsive events meaning that they always fire in response to other events.  When you exit a field, the Validate event for that field should fire.  My recommendation would be to leverage the Validate event for your validation as opposed to the Exit event and to ensure that each Validate event is set to run client-side (select "client" from the "Run At" option within the script window.

I apologize in advance if you are already familiar with the Validate event and it's proper usage but I'll mention it for completeness.  The Validate event can essentially have any type of script you'd like within but the goal is that it should return either "false" or "true".  To do this, however, the syntax is actually a bit different than you'd expect and is as follows:

if (this.rawValue == "A good value")

{

     true;

}

else

{

     false;

}

Every field has a property called "validationMessage".  This is the property that holds the string message that should be displayed upon a failed validation.  If you wanted to test for a null value in a text field and wanted a custom validation message, you would do something like the following:

if (this.rawValue == "")

{

     this.validationMessage = "You must enter a value before proceeding!";

     false;

}

else

{

     true;

}

Lastly, if you select the text field in question and go to the object palette where you can set validations, you'll notice two text fields towards the bottom:  one for a validation pattern message and one for a validation script message.  The "validationMessage" property of that text field is the same as the latter text field on the palette.  You'll also notice a checkbox atop the validation script message text field that reads "error".  Leaving this unchecked means that any validation script that runs and fails will be treated as a warning instead of an actual error.  This means that you will still be able to proceed, save the form, close the form, etc.  If you want to force the validation you need to check this option to denote that the validation must pass before the form can be completed.

Once again, I do apologize if you already know all of this.  I make no assumptions so I thought I'd start with the above.  If the above doesn't help, shoot me an email and I'll help you out!

Good luck!

Josh Boyle

Cardinal Solutions Group

jboyle@cardinalsolutions.com

Avatar

Level 1

Thank you, Josh.

This works just fine, except when the field is empty.  The script apparently will not run at all if the value is null UNLESS I have data in the field, and then I erase it.  I tried two ways to test for null (this.rawValue == null) and (this.rawValue == "").  Neither work.

I threw in an alert that should run each time the script runs.  It indicates that the script does not run when the field is empty.

var a = new RegExp();
a.compile("^[a-zA-Z\-]*$", "i");

app.alert("Validator");

var currentValue = this.rawValue;
if(currentValue == null) {
    this.fillColor = "255,128,128";
    this.validationMessage = "Please enter your last name.";
    false;
}
else if(a.test(currentValue) == false) {
    this.fillColor = "255,128,128";
    this.validationMessage = "Please correctly enter your last name.";
    false;
}
else {
    this.fillColor = "255,255,255";
    true;
}

Avatar

Former Community Member

Hmmm that's an interesting dilemma for sure!

Here's a workaround you can try:

1) Create a hidden field on your form (i.e. txtField)

2) On the Exit event of each field needing to be validated, set txtField.rawValue to this.name (this.name will get you the node's name)

3) On the Change event of txtField, have a simple script similar to as follows:

xfa.resolveNode(this.rawValue).execValidate();

The above should force the validate event to fire even when leaving the field without editing it at all.

As an alternative to the above, have you played around with setting the fields as mandatory as well?