Another variation:
/*
A breakdown of the ZIP code regular expression follows:
/^ # Assert position at the beginning of the string.
[0-9]{5} # Match a digit, exactly five times.
(?: # Group but don't capture...
- # Match a literal "-".
[0-9]{4} # Match a digit, exactly four times.
) # End the noncapturing group.
? # Repeat the preceding group between zero and one time.
$/ # Assert position at the end of the string.
*/
var RegExp_USZipCode_Valid = /^[0-9]{5}(?:-[0-9]{4})?$/; // US ZipCode 5 and 5+4 formats;
if(RegExp_USZipCode_Valid.test(event.value) == false) {
app.alert("Not a valid format for US Zip Code or US Zip Code + 4", 1, 0);
}
It is also possible to write a keystroke validation.