LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Cupcake Corner (Day 51): Address Validation--use Regex!

Forums > 100 Days of SwiftUI

For fun, I added additional code to check for valid address information in the Order class using regular expressions (see Paul's article at:

These are both US-centric, one for checking for a 5 digit Zip Code (not Zip+4!):

//confirm zip code is 5 digits long

let zipRange = NSRange(location: 0, length: zip.utf16.count)
let zipRegex = try! NSRegularExpression(pattern: "[0-9]{5}")
if zipRegex.firstMatch(in: zip, options: [], range: zipRange) == nil {
    return false

...the second is to perform a simple-minded address check: one or more digits, followed by one or more spaces, followed by at least one number or letter:

 //confirm address starts with numbers followed by something that looks like a street name

let streetAddressRange = NSRange(location: 0, length: streetAddress.utf16.count)
let streetAddressRegex = try! NSRegularExpression(pattern: "[0-9]+[ ]+[a-zA-Z0-9]+")
if streetAddressRegex.firstMatch(in: streetAddress, options: [], range: streetAddressRange) == nil {
    return false


1 suggestion, the street address would work for something like "1 street" but not for something like "1 st williams way". Adding the allowance of both a space and a - would be beneficial. That being said, using regular expressions to match a street address is hard, there are so many different ways they might be written which is 1 of the reasons many websites do things like getting you to enter the postcode/zip code and street number, using these 2 things combined will usually give a 1 to 1 match when looking up on an address database (obviously way beyond the scope of what you are doing).

Good work on the changes and thinking about what data should be validated


That regex would also not pass addresses like mine which contain something like "1/4".

But, really, regular expressions aren't a good choice for validating a street address, mostly because addresses have so much variety that they are anything but regular.

That said, good job! One thing you might want to try is the new RegexBuilder syntax.


check out What is the ultimate postal code and zip regex? on Stack overflow.

For UK can do a request "https: // / postcodes / (postcode)" and if you get back "status":200 then you have a valid postcode. is a free postcode lookup API and geocoder for the UK


Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.