How to check if the characters entered in the Swift Text Field are email addresses

How to check if the characters entered in the TextField are email addresses

This time, I would like to implement a method of conditional branching depending on whether the characters entered in the TextField are email addresses.

First of all, set up Label, TextField, Button

スクリーンショット 2020-12-24 15.47.40.png

Then connect item and code

スクリーンショット 2020-12-24 15.49.46.png

Add the code below

//Email address or not class
class func isValidMailAddress(_ string: String) -> Bool {
        let mailAddressRegEx = "[A-Z0-9a-z._+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let mailAddressTest = NSPredicate(format:"SELF MATCHES %@", mailAddressRegEx)
        let result = mailAddressTest.evaluate(with: string)
        return result
}

Connect Button with Touch Up Inside

スクリーンショット 2020-12-24 15.59.01.png

I will write the code here

Last spurt!

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var Label: UILabel!
    @IBOutlet weak var TextField: UITextField!
    @IBOutlet weak var Button: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }


    //Email address or not class
    class func isValidMailAddress(_ string: String) -> Bool {
            let mailAddressRegEx = "[A-Z0-9a-z._+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
            let mailAddressTest = NSPredicate(format:"SELF MATCHES %@", mailAddressRegEx)
            let result = mailAddressTest.evaluate(with: string)
            return result
     }


    @IBAction func ButtonTouchUpInside(_ sender: Any) {
        //When the email address is correct
        if ViewController.isValidMailAddress(self.TextField.text!) {
            Label.text = "Correct email address"
        //When the email address is incorrect
        } else {
            Label.text = "Please enter the correct email address"
        }
    }


}

Complete

ezgif-6-b32a736c76c7.gif

Recommended Posts

How to check if the characters entered in the Swift Text Field are email addresses
[Swift] How to display the entered characters in Widget via UserDefaults when using WidgetKit
How to display the text entered in text_area in Rails with line breaks
How to check the logs in the Docker container
How to add sound in the app (swift)
How to check Rails commands in the terminal
How to correctly check the local HTML file in the browser
Line breaks in the entered text are not displayed in view
Check if Ethereum transactions are not included in the block
[Swift] When you want to know if the number of characters in a String matches a certain number ...
How to store the information entered in textarea in a variable in the method
Determine if the strings to be compared are the same in Java
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
Process to check if "some fields are duplicated" from the Bean List
[Swift] How to set an image in the background without using UIImageView.
Android development, how to check null in the value of JSON object
[Ruby] Returns characters in a pyramid shape according to the entered numbers
[swift5] How to specify color in hexadecimal
Shorten the UUID to base64 in Swift.
[Swift] How to fix Label in UIPickerView
[Swift] How to implement the countdown function
How to get the date in java
How to overwrite Firebase data in Swift
[Swift] How to get the number of elements in an array (super basic)
If the parameter is an array, how to include it in Stopara's params.permit
What to do if the changes are not reflected in the jar manifest file
How to get the ID of a user authenticated with Firebase in Swift
How to check the latest version of io.spring.platform to describe in pom.xml of Spring (STS)
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
[Swift] How to link the app with Firebase
[Swift] How to implement the fade-in / out function
How to execute tasks in parallel in Swift in Swift Package
How to build the simplest blockchain in Ruby
What to do if you can't get the text of an element in Selenium
[Swift] How to simply describe dismiss that was not taught in the introductory book
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[rails devise] How to transition users who are not logged in to the login page
How to use UsageStatsManager in Android Studio (How to check the startup time of other apps)
How to check the WEB application created in the PC development environment on your smartphone
Use a binary search to see if there are any values in the array
How to implement UICollectionView in Swift with code only
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
[Java] How to omit the private constructor in Lombok
Organized how to interact with the JDK in stages
[SwiftUI] How to specify the abbreviated position of Text
[Swift] Use UserDefaults to save data in the app
How to specify the resource path in HTML import
If you want to recreate the instance in cloud9
How to debug the generated jar file in Eclipse
[Swift] How to get the document ID of Firebase
[Rails] How to display an image in the view
[Android] How to check if the Google Play developer service is installed when the app is launched
Memo that transitions to the login screen if you are not logged in with devise