Skip to main content
May 27, 2016 3:24 PM UTC
Answered

Canadian Postal Code custom validation script

Is there a custom validation script for a Canadian Postal Code (form was created in Acrobat XI)?  Thanks much.

ApeeMandyCorrect answer
Participant
Mar 19, 2025 5:52 PM UTC

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
}
}

9 replies

JR Boulay
Community Expert
Community Expert
Mar 29, 2025 11:24 AM UTC

[MOVED TO THE ACROBAT DISCUSSIONS]

Translate
Acrobate du PDF, InDesigner et Photoshopographe
ApeeMandyCorrect answer
Participant
Mar 19, 2025 5:52 PM UTC

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
}
}

Translate
May 31, 2016 1:14 PM UTC

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.

Translate
Inspiring
May 31, 2016 4:00 PM UTC

It should work. the length is not 6 but 7 since a space is a character.

Translate
Inspiring
May 27, 2016 5:13 PM UTC

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.

Translate
May 27, 2016 6:38 PM UTC

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.

Translate
Inspiring
May 27, 2016 7:45 PM UTC

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

Translate
try67
Community Expert
Community Expert
May 27, 2016 3:51 PM UTC

What's the format for these codes?

Translate