UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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: https://www.hackingwithswift.com/articles/108/how-to-use-regular-expressions-in-swift).

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
}

3      

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

3      

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.

3      

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

For UK can do a request "https: // api.postcodes.io / postcodes / (postcode)" and if you get back "status":200 then you have a valid postcode.

Postcodes.io is a free postcode lookup API and geocoder for the UK

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.