
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
I spent hours trying to find the perfect script that worked exactly as I wanted. I wanted to be able to enter it a1a1a1, A1A1A1 or a1a 1a1 and they all auto correct to A1A 1A1. This was a far bigger struggle than it should have been, ChatGPT and I had quite the morning. Anyway, I'm pasting my script here in case anyone else is struggling.
event.value = event.value.toUpperCase(); // Convert to uppercase
// If the field is empty, allow it without any further validation
if (event.value === "") {
event.rc = true;
} else {
// Automatically insert space after the third character if missing
if (event.value.length === 6) {
event.value = event.value.substring(0, 3) + " " + event.value.substring(3);
}
// Validate against the Canadian postal code format (excludes invalid letters: D, F, I, O, Q, U)
var pattern = /^(?!.*[DFIOQU])[A-VXY][0-9][A-Z] [0-9][A-Z][0-9]$/;
if (!pattern.test(event.value)) {
app.alert("Please enter a valid Canadian postal code (Format: A1A 1A1)", 3);
this.resetForm([event.target.name]); // Clears the field if invalid
}
}
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.
Copy link to clipboard
Copied
I spent hours trying to find the perfect script that worked exactly as I wanted. I wanted to be able to enter it a1a1a1, A1A1A1 or a1a 1a1 and they all auto correct to A1A 1A1. This was a far bigger struggle than it should have been, ChatGPT and I had quite the morning. Anyway, I'm pasting my script here in case anyone else is struggling.
event.value = event.value.toUpperCase(); // Convert to uppercase
// If the field is empty, allow it without any further validation
if (event.value === "") {
event.rc = true;
} else {
// Automatically insert space after the third character if missing
if (event.value.length === 6) {
event.value = event.value.substring(0, 3) + " " + event.value.substring(3);
}
// Validate against the Canadian postal code format (excludes invalid letters: D, F, I, O, Q, U)
var pattern = /^(?!.*[DFIOQU])[A-VXY][0-9][A-Z] [0-9][A-Z][0-9]$/;
if (!pattern.test(event.value)) {
app.alert("Please enter a valid Canadian postal code (Format: A1A 1A1)", 3);
this.resetForm([event.target.name]); // Clears the field if invalid
}
}
Copy link to clipboard
Copied
[MOVED TO THE ACROBAT DISCUSSIONS]
Acrobate du PDF, InDesigner et Photoshoptographe

