Introduction to email address validation in swift
In this tutorial, we will learn how to add email address validation in swift4. We will use NSPredicate with regex to validate email of format “xxx@xx.xxx”. Also, how to validate an empty UITextField in swift using guard statement. Let us jump to the tutorial and learn how to validate email address and check for empty UITextFields in swift.
Steps to add email validation in swift
Creating user interface for the app
Step 1: Open up Xcode and select “Single View App” template, name it “ValidationsAndEmailValidation“.
Step 2: Open “Main.storyboard” as we are going to create user interface as shown in given below picture
Step 3: Drag UILabel to view as we want to display validation message to user if inputs failed to validate email address or if UITextFields are empty i.e empty UITextField validation and add constraints as shown below
Step 4: Drag UITextField for Email address to be entered by user and add constraints as shown below
Step 5: Drag UITextField for password to be entered by user and add constraints as shown below
Step 6: Drag UIButton so that we can validate the values inputted by user when he tap on button and add constraints as shown below
Step 7: As of now we are done with our user interface design and if you run your app, you will see its nicely comes up on iPhone simulator screen.
Coding for email validation
Step 8: Open “ViewController.swift” and create IBOutlet for two UITextFields, UILabel and IBAction for our UIButton.
// | |
// ViewController.swift | |
// ValidationsAndEmailValidation | |
// | |
// Created by Aman Aggarwal on 26/01/18. | |
// Copyright © 2018 Aman Aggarwal. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var txtEmail: UITextField! | |
@IBOutlet weak var txtPassword: UITextField! | |
@IBOutlet weak var lblValidationMessage: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
//MARK:- loginUser | |
@IBAction func loginUser(_ sender: Any) { | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
Step 9: Open “Main.storyboard” and connect IBOutlets created in step 8 to UILabel and UITextFields. Connect IBAction to “touchupinside” event of UIButton.
Step 10: Now in viewDidLoad function set “lblValidationMessage” to hidden as we did not want to show any message until user tap on UIButton.
// | |
// ViewController.swift | |
// ValidationsAndEmailValidation | |
// | |
// Created by Aman Aggarwal on 26/01/18. | |
// Copyright © 2018 Aman Aggarwal. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var txtEmail: UITextField! | |
@IBOutlet weak var txtPassword: UITextField! | |
@IBOutlet weak var lblValidationMessage: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
lblValidationMessage.isHidden = true | |
} | |
//MARK:- loginUser | |
@IBAction func loginUser(_ sender: Any) { | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} |
func isValidEmail(emailID:String) -> Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" | |
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
return emailTest.evaluate(with: emailID) | |
} |
Step 12: In our “loginUser” IBAction, we will call the above script and validate our email typed in “txtEmail” UITextField. Also we will use “guard” statement to check whether our UITextFields contains any value or are empty. If empty then we will show validation message in “lblValidationMessage” UILabel.
//MARK:- loginUser | |
@IBAction func loginUser(_ sender: Any) { | |
lblValidationMessage.isHidden = true | |
guard let email = txtEmail.text, txtEmail.text?.characters.count != 0 else { | |
lblValidationMessage.isHidden = false | |
lblValidationMessage.text = "Please enter your email" | |
return | |
} | |
if isValidEmail(emailID: email) == false { | |
lblValidationMessage.isHidden = false | |
lblValidationMessage.text = "Please enter valid email address" | |
} | |
guard let password = txtPassword.text, txtPassword.text?.characters.count != 0 else { | |
lblValidationMessage.isHidden = false | |
lblValidationMessage.text = "Please enter your password" | |
return | |
} | |
} |
Step 13: Run your app and you will see the validations are working.
Complete Code:
// | |
// ViewController.swift | |
// ValidationsAndEmailValidation | |
// | |
// Created by Aman Aggarwal on 26/01/18. | |
// Copyright © 2018 Aman Aggarwal. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var txtEmail: UITextField! | |
@IBOutlet weak var txtPassword: UITextField! | |
@IBOutlet weak var lblValidationMessage: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
lblValidationMessage.isHidden = true | |
} | |
//MARK:- loginUser | |
@IBAction func loginUser(_ sender: Any) { | |
lblValidationMessage.isHidden = true | |
guard let email = txtEmail.text, txtEmail.text?.characters.count != 0 else { | |
lblValidationMessage.isHidden = false | |
lblValidationMessage.text = "Please enter your email" | |
return | |
} | |
if isValidEmail(emailID: email) == false { | |
lblValidationMessage.isHidden = false | |
lblValidationMessage.text = "Please enter valid email address" | |
} | |
guard let password = txtPassword.text, txtPassword.text?.characters.count != 0 else { | |
lblValidationMessage.isHidden = false | |
lblValidationMessage.text = "Please enter your password" | |
return | |
} | |
} | |
func isValidEmail(emailID:String) -> Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" | |
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
return emailTest.evaluate(with: emailID) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
} | |
Where to go from here:
We learned how to validate email address in swift. Also we learned to validate empty UITextField using guard statement in swift. Download the source code from here.