Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Canadian Postal Code custom validation script

Guest
May 27, 2016 May 27, 2016

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

TOPICS
PDF forms
6.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
New Here ,
Mar 19, 2025 Mar 19, 2025

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

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 27, 2016 May 27, 2016

What's the format for these codes?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2016 May 27, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 27, 2016 May 27, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 27, 2016 May 27, 2016

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2016 May 31, 2016

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 31, 2016 May 31, 2016

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. 

Cdn PC sample.jpg

Thanks, gkaiseril.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2016 May 31, 2016

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

CombField.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 19, 2025 Mar 19, 2025

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 29, 2025 Mar 29, 2025
LATEST

[MOVED TO THE ACROBAT DISCUSSIONS]


Acrobate du PDF, InDesigner et Photoshoptographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines