Postcode must be four digits. Optionally checks it against a state field (QLD 4xxx/9xxx, NSW 1xxx/2xxx, ACT 02xx/26xx/29xx, VIC 3xxx/8xxx, TAS 7xxx, SA 5xxx, WA 6xxx, NT 08xx/09xx).
How to use / view script
Postcode field, Validate > Run custom validation script:
FS_ValidatePostcode();
To also check against a state field (values such as QLD, NSW):
FS_ValidatePostcode("State");
/* @name FS_Postcode
@title Australian postcode validation
@category Validation
@order 40
@description Postcode must be four digits. Optionally checks it against a state field (QLD 4xxx/9xxx, NSW 1xxx/2xxx, ACT 02xx/26xx/29xx, VIC 3xxx/8xxx, TAS 7xxx, SA 5xxx, WA 6xxx, NT 08xx/09xx).
@usage Postcode field, Validate > Run custom validation script:
FS_ValidatePostcode();
To also check against a state field (values such as QLD, NSW):
FS_ValidatePostcode("State");
*/
function FS_PostcodeMatchesState(pc, st) {
var n = Number(pc);
st = String(st).toUpperCase().replace(/[^A-Z]/g, "");
var r = {
NSW: [[1000, 2599], [2619, 2899], [2921, 2999]],
ACT: [[200, 299], [2600, 2618], [2900, 2920]],
VIC: [[3000, 3999], [8000, 8999]],
QLD: [[4000, 4999], [9000, 9999]],
SA: [[5000, 5999]],
WA: [[6000, 6999]],
TAS: [[7000, 7999]],
NT: [[800, 999]]
}[st];
if (!r) return true; // unknown or blank state: don't block
for (var i = 0; i < r.length; i++) if (n >= r[i][0] && n <= r[i][1]) return true;
return false;
}
function FS_ValidatePostcode(stateFieldName) {
var v = String(event.value).replace(/\s/g, "");
event.value = v;
if (v === "") return;
if (!/^\d{4}$/.test(v)) {
app.alert("Please enter a four-digit postcode.", 1);
event.rc = false;
return;
}
if (stateFieldName) {
var f = event.target.doc.getField(stateFieldName);
if (f && f.valueAsString !== "" && !FS_PostcodeMatchesState(v, f.valueAsString)) {
app.alert("Postcode " + v + " doesn't match the state " + f.valueAsString + ". Please check both.", 1);
event.rc = false;
}
}
}