0

I have created the following code where I have an outlet name password, when the user enters a password, I seek to have a limit of more than 5 characters and less than 15. How can I impose this limit within this code?

    guard let password = password.text, password.count > 6 else {
        self.password.showError(true)
        return
    }

    guard (password == confirmPassword.text) else {
        self.confirmPassword.showError(true)
        return
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
jdanvz
  • 44
  • 1
  • 8

1 Answers1

2

You can do

 guard let password = password.text, password.count > 5 , password.count < 15 else {
    self.password.showError(true)
    return
}

or

guard (6...15).contains(password.text!.count) else {
    self.password.showError(true)
    return
}

Also you can use shouldChangeCharactersIn delegate method of the UITextField to limit the count check This

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87