Ika Venne
Ika Venne

Reputation: 35

how to make a password confirmer swift 3

I have 4 text fields, and 2 of them are password and confirm password. I need to check they are they same or have text in them before proceeding into the login screen. Heres my code:

func registerButtonTapped() {
    var a = false
    var b = false

    if passwordFieldR.text! == confirmFieldR.text! {
        a = true
    } else {
        nonMatchingPasswords.text = "Passwords Do Not Match"
    }

    if(passwordFieldR.text! == "" || confirmFieldR.text! == "") {
        nonMatchingPasswords.text = "Password Field is empty"
    } else {
        b = true    
    }

    if a == true && b == true {
        func registerButtonTwo(_ sender: Any) {
            self.performSegue(withIdentifier: "registertologin", sender: self)
        }
    }
}

The app crashes and gives me this error message in the console:

2017-06-19 21:29:20.926579+1200 Altitude[1655:588172] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-06-19 21:29:20.927965+1200 Altitude[1655:588172] [MC] Reading from public effective user settings.
2017-06-19 21:29:30.784007+1200 Altitude[1655:588172] -[Altitude.secondscreenviewcontroller registerButtonTwo:]: unrecognized selector sent to instance 0x100d085d0
2017-06-19 21:29:30.784671+1200 Altitude[1655:588172] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Altitude.secondscreenviewcontroller registerButtonTwo:]: unrecognized selector sent to instance 0x100d085d0'
*** First throw call stack:
(0x18add2fd8 0x189834538 0x18add9ef4 0x18add6f4c 0x18acd2d2c 0x190f370ec 0x190f3706c 0x190f215e0 0x190f36950 0x190f3646c 0x190f31804 0x190f02418 0x1916fbf64 0x1916f66c0 0x1916f6aec 0x18ad81424 0x18ad80d94 0x18ad7e9a0 0x18acaed94 0x18c718074 0x190f67130 0x1000e075c 0x189cbd59c)
libc++abi.dylib: terminating with uncaught exception of type NSException

What is happening and how can I fix this?

Upvotes: 0

Views: 2524

Answers (5)

Praveen Kumar
Praveen Kumar

Reputation: 29

Validate password and confirm password

func isValidPassword(passworld: String, confirmPassworld: String) -> Bool{
    if confirmPassworld.lowercased() == passworld.lowercased(){
        return true
    }
    return false
}

Upvotes: 0

vadian
vadian

Reputation: 285069

The error occurs because target / action methods must be declared on the top level of the class.

You probably mean

func registerButtonTapped() {

    var a = false
    var b = false

    if passwordFieldR.text! == confirmFieldR.text! {
        a = true
    } else {
        nonMatchingPasswords.text = "Passwords Do Not Match"
    }

    if(passwordFieldR.text! == "" || confirmFieldR.text! == "") {
        nonMatchingPasswords.text = "Password Field is empty"
    } else {
        b = true
    }

    if a == true && b == true {
       registerButtonTwo(self)
    }
}

func registerButtonTwo(_ sender: Any) {
     self.performSegue(withIdentifier: "registertologin", sender: self)
}

PS: registerButtonTapped() can be written in a more efficient way

func registerButtonTapped() {

    guard let password = passwordFieldR.text, !password.isEmpty,
        let confirm = confirmFieldR.text, !confirm.isEmpty else {
            nonMatchingPasswords.text = "Password Field is empty"
            return
    }
    guard password == confirm else {
        nonMatchingPasswords.text = "Passwords Do Not Match"
        return
    }
    registerButtonTwo(self)

}

Upvotes: 0

Harikrishnan R
Harikrishnan R

Reputation: 150

Replace

func registerButtonTwo(_ sender: Any) {
            self.performSegue(withIdentifier: "registertologin", sender: self)
        }

with just

self.performSegue(withIdentifier: "registertologin", sender: self)

Upvotes: 0

Saeed Rahmatolahi
Saeed Rahmatolahi

Reputation: 1338

I think It will Be useful for You (You can change 5 number to any Number that you want to be minimum of the password characters)

if  ((passwordFieldR.text?.characters.count)! > 5) {


 if passwordFieldR.text! == confirmFieldR.text! {


    print("registertologin")

 } else {

   print("Password Does Not Match Confirm Password")


 }

 } else {

  print("password Characters should be more Than 5")

  }

Upvotes: 1

mohak
mohak

Reputation: 623

It seems like you have a dangling connection from storyboard that you deleted from your code but forgot to do so in the storyboard. Right click on the view (probably the login button) and check for a dead connection.

Upvotes: 0

Related Questions