There are several interesting scripting validation techniques that can be demonstrated in the context of a Canadian postal code field. While the subject domain is fairly specific, the techniques used are applicable to other field validations.
The challenge is to get an accurate and user-friendly data capture experience for a postal code. Some of the considerations:
- Make sure the field value conforms to the rules for Canadian postal codes (use regular expressions for validating)
- Make sure that error messages are as specific as possible to assist better user input (customize validation error messages)
- Make sure the user can optionally key in a space inside their postal code (use an edit picture clause)
- Make sure the postal code is entered in upper case (modify keystrokes on the change event)
- Make sure the postal code value is consistent with other address fields: province and city (inter-field validation)
Canadian Postal Code Rules
The default way to validate a postal code is to use a validation picture clause: text{A9A 9A9}. However, this picture allows many illegal postal codes. For example, the meta character “A” in a picture clause allows any Unicode letter value – whereas a postal code letter is far more restricted:- The first character of a Canadian postal code corresponds to a geographic region and is limited to the set of characters: ABCEGHJKLMNPRSTVXY
- The third and fifth characters may not include the letters D, F, I, O, Q or U (because they are hard for OCR software to deal with).
if (vTestValue.charAt(0).match(/[ABCEGHJKLMNPRSTVXY]/) == null) { … error condition … }
Specific Error Messages
When the user enters an invalid postal code, be as specific as possible in telling them what to fix. There is a world of difference between: “Invalid postal code” and “Second, fourth and sixth postal characters must be numeric” For the person who typed in a letter “O” instead of a “0″ (zero), this will be the clue they need to correct their error. To make this work, validate the portions of the postal code individually and then set the field error message accordingly:var vNums = vTestValue.charAt(1) +Note that you might choose to store error messages as form variables. That way they will be examined by Designer’s spell checker.
vTestValue.charAt(3) +
vTestValue.charAt(5);
if (vNums.match(/[0-9][0-9][0-9]/) == null)
{
vTestField.validationMessage =
“Second, fourth and sixth postal characters must be numeric”;
return false;
}
The Optional Space
The user ought to be able to enter their data with or without a space – but we want to store it consistently – without the space. To accomplish this, use an edit picture clause: text{OOO OOO} Edit picture clauses are used by Reader to:- format the raw value for editing when the user enters the field
- parse the edited value to set the raw value when the user exits the field
Upper Case Only
The easiest way to force the value to upper case is to trap the characters as the user enters them and convert them as they type. We do this with a very simple change event:form1.address.PostalCode::change – (JavaScript, client) // Change all user entered characters to upper case. xfa.event.change = xfa.event.change.toUpperCase();Because we know that the input data is in upper case, it makes our validation logic cleaner, as it needs only be concerned with the upper case variations.

Hi – this is very good, but all I need to do is to validate for the city of Edmonton that the Postal Codes begin with T5 or T6.Would you know the code for that?Thanks – Wendy
Wendy:This form will auto-fill the city name where it’s obvious (e.g. “M” codes are for Toronto).We can do this for the Edmonton codes by modifying the validateCanadianPostalCode() function:case ‘T’:if (vTestValue.substring(0,2) == “T5″ || vTestValue.substring(0,2) == “T6″) {vCity.rawValue = “Edmonton”;}vProvince.rawValue = “AB”break;We could make the solution more general purpose by building a lookup table of all the forward sortation areas. e.g. based on:http://en.wikipedia.org/wiki/List_of_T_postal_codes_of_CanadaJohn