[Solved] Validating Password in Single Text Field Swift [closed]


Create a Label, TextField and a Button, Add them all to your view controller and create a IBAction for the Button. In the Attribute Inspector set the tag number of the button to 2, you can also change the TextFields Keyboard type to Number Pad.

Setup

Now add three variables:

var Password1: String?
var Password2: String?
var Offical_Password: String?

In the ViewDidLoad function set the buttons tag equal to 1:

Button.tag = 1

These two functions shown below will handle the settings of the TextField and the Label. It will also check whether Password1 is equal to Password2 and then set the Official_Password:

    func Password () {
    if (TextField.text == "") {
        // Password is required
    } else {
        Lable.text = "Confirm Password"
        Password1 = TextField.text
        TextField.text = ""
    }
}


func Password_Confimed () {
    if TextField.text == "" {
        // Confirmation Password is required
    } else {
        Password2 = TextField.text
    }
    if Password1 == Password2 {
        Lable.text = "Done"
        Offical_Password = Password1
        TextField.text = ""
    } else {
        // Handle error
    }
}

Finally in the IBAction function add:

@IBAction func Button_Pressed(sender: AnyObject) {
    if Button.tag == 1 {
        Password()
        Button.tag = 2
    } else if Button.tag == 2 {
        Password_Confimed()
        // Go to another View Controller
    }
}

The Final Result

You will have to add how you want to deal with problems such as making sure the password isn’t too weak and what you actually want to do when the password is set.

3

solved Validating Password in Single Text Field Swift [closed]