Reference · Parsing, formatting and shared types
validatePostalCodefunction
Checks the shape of a US ZIP code or a Canadian postal code.
Import it
import { validatePostalCode } from "@johnmorrisdotca/address-plus";
Definition
function validatePostalCode(code: string): PostalValidationResult
Parameters
| Name | Type | Description |
|---|---|---|
code | string | The code, with or without the space or the ZIP+4. |
Returns
PostalValidationResult
Whether it is well formed, its type (zip or postal), and the code written the standard way.
Examples
import { validatePostalCode } from "@johnmorrisdotca/address-plus";
console.log(validatePostalCode("k1a0b1"));
// → {"isValid":true,"type":"postal","formatted":"K1A 0B1","message":"Valid Canadian postal code format"}{
isValid: true,
type: 'postal',
formatted: 'K1A 0B1',
message: 'Valid Canadian postal code format'
}What it prints (run when this page was built)