regex - One Regular Expression to validate US and Canada ZIP / Postal Code -
i developing stationery program. customers have choice pick region either or canada. when enter address have enter zip/postal code. trying validate field cannot use reg exp either or canada. require regular expression validates both country zip code.
not knowing language you're using, not use abbreviations character classes:
^[0-9]{5}$|^[a-z][0-9][a-z] ?[0-9][a-z][0-9]$
depending on language, might able abbreviate to
^([0-9]{5}|[a-z][0-9][a-z] ?[0-9][a-z][0-9])$
or
^(\d{5}|[a-z]\d[a-z] ?\d[a-z]\d)$
to support zip+4:
^(\d{5}(-\d{4})?|[a-z]\d[a-z] ?\d[a-z]\d)$
and if want picky canada codes:
^(\d{5}(-\d{4})?|[a-ceghj-nprstvxy]\d[a-ceghj-nprstv-z] ?\d[a-ceghj-nprstv-z]\d)$
Comments
Post a Comment