Copy link to clipboard
Copied
Is there a custom validation script for a Canadian Postal Code (form was created in Acrobat XI)? Thanks much.
Copy link to clipboard
Copied
What's the format for these codes?
Copy link to clipboard
Copied
One possible solution that includes validation for the Postal District, the Forward Station Area, and the Local Delivery Area:
event.value = event.value.toUpperCase();
if(event.value != "")
{
// validate Canadian postal code;
event.rc = /^(?!.*[DFIOQU])[A-VXY][0-9][A-Z] [0-9][A-Z][0-9]$/.test(event.value);
if(event.rc == false)
{
app.alert("Please enter a correct Canadian Postal code", 1, 0);
this.resetForm([event.target.name]);
}
}
Note there are only 18 valid Postal Districts. The negative look ahead eliminates the 6 alphabetical characters form anywhere in the string.
See Postal Codes in Canada for more information about the structure of the Canadian Postal Code.
The form field is reset incase a previous correct Postal Code is replaced with an invalid code and the correct Postal Code is not retained as the value of the field.
Copy link to clipboard
Copied
Hi gkaiseril,
Thanks very much for your reply. I don't know JavaScript - but it doesn't seem to matter what postal code I enter, I get an error message; and the letters don't appear in uppercase.
Copy link to clipboard
Copied
I have tried the form with the posted code using Acrobat and Adobe Reader without problems.
One needs to use a product that can run the validation code and mobile devices probably cannot as well as many non-Adobe products.
A working sample form Canadian Postal Code Validation
Copy link to clipboard
Copied
HI gkaiseril,
I'm using Acrobat XI Pro on a desktop computer; the OS is Windows 7 Enterprise (64-bit). I wish I knew how to attach a sample page of the PDF so that you could see how the code works on my end but it looks like only images or videos can be uploaded here.
Anyway, I've entered the JavaScript (exactly as you posted it) under the Validate tab as a "custom validation script". However, as I mentioned, it doesn't matter what postal code I enter, an error message pops up.
The only other text field property that's set is under the Options tab - "comb 6 characters" (I doubt that would be affecting the script).
Thanks again,
Marquin
Copy link to clipboard
Copied
Okay, I finally got it to work. I had to remove "comb 6 characters" under the Appearance tab. The Postal Code fields in this form have separate slots for each character -- this is essentially for users who prefer to print and complete the form by hand. Unfortunately the Postal Code JavaScript won't work with a "comb of characters"; so I set the alignment to "center" instead. It looks a little awkward -- but it works. Attached is a screenshot.
Thanks, gkaiseril.
Copy link to clipboard
Copied
It should work. the length is not 6 but 7 since a space is a character.