A validator for postal codes with support for 200+ regions

Overview

PostalCodeValidator

Build Status License Swift Version

A validator for postal codes with support for 200+ regions.

import Foundation
import PostalCodeValidator

if 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.

Requirements

  • Swift 4.0+

Installation

Swift Package Manager

Add the PostalCodeValidator package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/FormatterKit/PostalCodeValidator",
        from: "0.1.0"
    ),
  ]
)

Then run the swift build command to build your project.

Known Issues

  • Armed forces in Germany (and likely other countries) use special postal codes, which aren't included in the upstream data source. This will cause validation of these postal codes to fail. (Thanks for pointing this out, @timohetzel)

Additional Details

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, PostalCodeValidator sources its information from http://i18napis.appspot.com. The most recent release of this software uses information retrieved from the website on November 27, 2019.

License

MIT

Contact

Mattt (@mattt)

You might also like...
Effective DI library for rapid development in 200 lines of code.
Effective DI library for rapid development in 200 lines of code.

Effective DI library for rapid development in 200 lines of code.

Swift Validator is a rule-based validation library for Swift.
Swift Validator is a rule-based validation library for Swift.

Swift Validator is a rule-based validation library for Swift. Core Concepts UITextField + [Rule] + (and optional error UILabel) go into

A Payment Card UI & Validator for iOS
A Payment Card UI & Validator for iOS

Description Caishen provides an easy-to-use text field to ask users for payment card information and to validate the input. It serves a similar purpos

Declarative form validator for SwiftUI.
Declarative form validator for SwiftUI.

SwiftUIFormValidator The world's easiest, most clean SwiftUI form validation. SwiftUIFormValidator A declarative SwiftUI form validation. Clean, simpl

Swift String Validator. Simple lib for ios to validate string and UITextFields text for some criterias

Swift String validator About Library for easy and fastest string validation based on сciterias. Instalation KKStringValidator is available through Coc

SwiftEmailValidator - A Swift implementation of an international email address syntax validator based on RFC5321 & RFC5322

SwiftEmailValidator A Swift implementation of an international email address syn

Swiftly convert country codes and cultural terms to all 269 emoji flags without hassle
Swiftly convert country codes and cultural terms to all 269 emoji flags without hassle

Swiftly convert country codes and cultural terms to all 269 emoji flags without hassle

A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy!

Colors A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy! Note: Colors master requires Xcode 7.3

The Bloc Pattern is a way to separate UI and Logic in SwiftUI codes;

The Bloc Pattern is a way to separate UI and Logic in SwiftUI codes. The Bloc is like a state machine where it accepts an event and produce a state.

With QRKit you can easily create QR or bar codes in Swift.

With QRKit you can easily create QR or bar codes in Swift. You can choose foreground and background colors. The QR code you get simply as UIImage.

AppKit Carbon key codes to UIKey-compatible enums

KeyCodes Versions of UIKey, UIKeyboardHIDUsage, and UIKeyModifierFlags that work with AppKit's NSEvent. No need for Carbon.HIToolbox. Aside from being

An easy way to access reviews for your app instead of writing repetitive and redundant codes for every app.

AppStoreReviewManager An easy way to access reviews for your app instead of writing repetitive and redundant codes for every app. Requirements iOS 9.0

A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy!

Colors A pure Swift library for using ANSI codes. Basically makes command-line coloring and styling very easy! Note: Colors master requires Xcode 7.3

send your valuable codes here.

Hacktoberfest2021 # Hacktoberfest 2021 Hacktoberfest encourages participation in the open source community, which grows bigger every year. Complete th

A customizable verification code textField. Can be used for phone verification codes, passwords etc
A customizable verification code textField. Can be used for phone verification codes, passwords etc

KKPinCodeTextField A customizable verification code textField. Can be used for phone verification codes, passwords etc. Настраиваемое текстовое поле д

Convert Countries, Country Codes, Continents,... with a single line of code.
Convert Countries, Country Codes, Continents,... with a single line of code.

iOS · macOS · watchOS · tvOS A pure Swift library that allows you to easily convert Countries to Country Codes and Country Codes to Countries. There i

A category on NSString to convert Emoji Cheat Sheet codes to their equivalent Unicode characters

NSString+Emojize A category on NSString to turn codes from Emoji Cheat Sheet into Unicode emoji characters. Getting Started In order to use NSString+E

Autogenerated Twitter v2 API codes for iOS

Swift5 API client for OpenAPIClient Twitter API v2 available endpoints Overview This API client was generated by the OpenAPI Generator project. By usi

Oak is an iOS app built with SwiftUI for managing your 2FA codes
Oak is an iOS app built with SwiftUI for managing your 2FA codes

🔒 📱 OakOTP OakOTP is an iOS app built with SwiftUI for managing your 2FA codes. Supports iPhone & iPad Supports scanning a QR code or entering crede

Comments
  • Extra

    Extra ")"'s in example code

    if 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
    

    } should be if 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
    

    }

    opened by perbrondum 1
Owner
FormatterKit
`String(format:)` for the sophisticated hacker set
FormatterKit
A category on NSString to convert Emoji Cheat Sheet codes to their equivalent Unicode characters

NSString+Emojize A category on NSString to turn codes from Emoji Cheat Sheet into Unicode emoji characters. Getting Started In order to use NSString+E

DIY.org 643 Sep 8, 2022
Easiest way to create an attributed UITextView (with support for multiple links and from html)

AttributedTextView Easiest way to create an attributed UITextView (with support for multiple links and html). See the demo app and the playground for

Edwin Vermeer 430 Nov 24, 2022
`resultBuilder` support for `swift-markdown`

SwiftMarkdownBuilder resultBuilder support for swift-markdown. The default way to build Markdown in swift-markdown is to use varargs initializers, e.g

DocZ 9 May 31, 2022
A simple library that provides standard Unicode emoji support across all platforms

Twitter Emoji (Twemoji) A simple library that provides standard Unicode emoji support across all platforms. Twemoji v13.1 adheres to the Unicode 13.0

Twitter 15k Jan 8, 2023
🔥 🔥 🔥Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

?? ?? ??Support for ORM operation,Customize the PQL syntax for quick queries,Support dynamic query,Secure thread protection mechanism,Support native operation,Support for XML configuration operations,Support compression, backup, porting MySQL, SQL Server operation,Support transaction operations.

null 60 Dec 12, 2022
A customisable view for entering arbitrary length pins, codes or passwords in iOS. Supports iOS 12 one time codes.

CBPinEntryView CBPinEntryView is a view written in Swift to allow easy and slick entry of pins, codes or passwords. It allows backspacing, dismissal o

Chris Byatt 183 Dec 5, 2022
CHIOTPField is a set of textfields that can be used for One-time passwords, SMS codes, PIN codes, etc. Mady by @ChiliLabs - https://chililabs.io

CHIOTPField CHIOTPField is a set of textfields that can be used for One-time passwords, SMS codes, PIN codes, etc. All fields support insertion of one

Chili Labs 241 Dec 13, 2022
Postal is a swift framework providing simple access to common email providers.

Postal is a swift framework providing simple access to common email providers. Example Connect let postal = Postal(configuration: .icloud(login: "myem

Snips 644 Dec 23, 2022
WeatherDiscussions - Select NOAA regions and display weather discussions for that region

An example of the core concepts of iOS development This is a simple three screen

Daniel 0 Feb 17, 2022
Dash-iOS - Dash gives your iPad and iPhone instant offline access to 200+ API documentation sets

Discontinued Dash for iOS was discontinued. Please check out Dash for macOS instead. Dash for iOS Dash gives your iPad and iPhone instant offline acce

Bogdan Popescu 7.1k Dec 29, 2022