A validator for postal codes with support for 200+ regions
A validator for postal codes with support for 200+ regions.
import Foundation import PostalCodeValidatorif let validator = PostalCodeValidator(locale: .current) { // United States (US) validator.validate(postalCode: "95014") // true validator.validate(postalCode: "22162-1010") // true
validator.validate(postalCode: "1234567") // false validator.validate(postalCode: "INVALID") // false
}
// 248 regions supported if let validator = PostalCodeValidator(regionCode: "JP") { // Japan (JP) validator.validate(postalCode: "154-0023") // true validator.validate(postalCode: "12345") // false }
Note: Only the region of a locale is used to determine validation rules. To validate the postal code for a particular region, you can construct a locale using an identifier with the
"und"language code.
Add the PostalCodeValidator package to your target dependencies in
Package.swift:
import PackageDescriptionlet package = Package( name: "YourProject", dependencies: [ .package( url: "https://github.com/FormatterKit/PostalCodeValidator", from: "0.1.0" ), ] )
Then run the
swift buildcommand to build your project.
The CLDR previously maintained a list of postal code formats, but that information was deprecated in v27.
Following the guidance of the twitter-cldr-rb project,
PostalCodeValidatorsources its information from http://i18napis.appspot.com. The most recent release of this software uses information retrieved from the website on November 27, 2019.
MIT
Mattt (@mattt)