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.
Is there a custom validation script for a Canadian Postal Code (form was created in Acrobat XI)? Thanks much.
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
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.