cancel
Showing results for 
Search instead for 
Did you mean: 

FormCalc: Validation Pattern & Message

Former Member
0 Kudos

Hello,

In my form I need to have a script for a field which allows Postal Codes for CDN/Zip Codes for the US. 

This script needs to check the value of my country field and if it is the US then it should only allow an entry of text{99999}and if anyone enters anything different then it should come up with a message saying entry can only be ##### or something like that.  If it is Canada then it should check that the pattern being entered is text{A9A 9A9} and then of course a message of some sort.

I know this is easy to do within the form, however becuase I need to check the country and then apply different rules I am not quite sure how to go about it.  If you have any ideas please let me know ... actual script examples would be appreciated.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Janice,

Basically you want a postal code text field to do different validation based on country value.

1. When your country value changed, do this javascript scripts to change the postal code patterns and validation message:

if (country.rawValue == "CN") {

this.format.picture.value = " text{A9A 9A9}"; //to change the patterns

this.validationMessage = "Invalid postal code for Canada" //to change the validation message

}

elseif ( .. ) { // US or some others countries in your country list

..

}

else{ // any others country

..

}

Or if your country code defaulted when the form is loaded, do above code in initialize* event then.

2. Under the validate* event of postal code text field, add this javascript

this.rawValue !== this.formattedValue; //check if the entered value align with the pattern

Thanks. I have tested above 3 statements are working.

You just need to combine them together to do the magic for you.

regards,

Xiang Li

Former Member
0 Kudos

Thx for your help.    I have been fighting with this thing trying your examples and I cannot get it to work.  I don't know Java at all and I am new to Adobe so this I'm sure does help.

Here is what I have in the Country Change:

data.#subform[0].Contact02Subform.CNTRYDRPDWN2::change - (JavaScript, client)


if (CNTRYDRPDWN2.rawValue == "CA") {

"xfa.form.data.#subform[0].Contact02Subform.PSTLZ".format.picture.value

= "text{A9A 9A9}"; //to change the patterns

"xfa.form.data.#subform[0].Contact02Subform.PSTLZ".this.validationMessage

= "Invalid postal code for Canada"; //to change the validation message

}

else if (CNTRYDRPDWN2.rawValue == "US") {

"xfa.form.data.#subform[0].Contact02Subform.PSTLZ".format.picture.value

= "text{99999}"; //to change the patterns

"xfa.form.data.#subform[0].Contact02Subform.PSTLZ".this.validationMessage

= "Invalid zip code for the US"; //to change the validation message

}

and then this in the Postal Code Validate

data.#subform[0].Contact02Subform.PSTLZ::validate - (FormCalc, client)


this.RawValue!==this.formattedValue; // check if entered value aligns with the pattern

When I run the form I get a script failed Error: accessor 'thisRawValue' is unknown.  I have not been able to fix it or even validate somehow if the rest of the code I put in is working.  I did try replacing the "this" with "xfa.form.data.#subform[0].Contact02Subform.PSTLZ" and that did not work either.

Any suggestions?

Former Member
0 Kudos

My Script failed error was becuase I had it set to FormClac.  Changed it to Java and now I get no error, but the validation, patters, message etc does not seen to be working at all.  Nothing gets applied/triggered.   Help please?  thx

Former Member
0 Kudos

o.k. .... after much thought and fighting with code I decide to abandon this effort.  I do thank you for your info.  I decided to approach it from a simpler standpoint and just have the postal code validation set to both patterns and then my inbound handler will send any errors back to the user in an email for them to deal with.  thx for your time. 

Former Member
0 Kudos

Hi Janice,

So sorry for the delay. Glad to hear you have your own solution.

You did it correctly, however I found there referenced variable needed correction.

You might want to try this out when you are free.

data.#subform[0].Contact02Subform.CNTRYDRPDWN2::change - (JavaScript, client)


if (this.rawValue == "CA") {

data.#subform[0].Contact02Subform.PSTLZ.format.picture.value

= "text{A9A 9A9}"; //to change the patterns

data.#subform[0].Contact02Subform.PSTLZ.this.validationMessage

= "Invalid postal code for Canada"; //to change the validation message

}

else if (this.rawValue == "US") {

data.#subform[0].Contact02Subform.PSTLZ.format.picture.value

= "text{99999}"; //to change the patterns

data.#subform[0].Contact02Subform.PSTLZ.this.validationMessage

= "Invalid zip code for the US"; //to change the validation message

}

I have never come across #subform[0] , i think you have different subform with same name and system created an array for you.

If #subform[0] is not working, try remove the #

I understand it is painful to code in javascript/formcalc without knowing is there any error in scripting.

Normally what i do to make sure I refer to the right variable, I check using popup message, example

xfa.host.messageBox(data.#subform[0].Contact02Subform.PSTLZ.format.picture.value);

xfa.host.messageBox(this.rawValue);

Hope this help. Thanks.

regards,

Xiang Li

Answers (1)

Answers (1)

0 Kudos

Hi,

The validation message can be changed at run time, but not sure if the validation pattern can be changed based on another field value(Country in this case).

Can you try using regular expressions in java script(you can try in validate event) with the examples given in these links:

http://stackoverflow.com/questions/160550/zip-code-us-postal-code-validation

http://www.itsalif.info/content/canada-postal-code-us-zip-code-regex

Regards,

Sathya.