Please tell me, I have a TextField in which I enter the password. After adding a new character, I need to check that the password meets certain requirements (for example, 8 characters). How can this be achieved? Thank you in advance)

    3 answers 3

    The easiest option:

    one

    enter image description here

    2 Set the type of Connection - Action , Type - UITextField , Event - Editing Changed .

    enter image description here

    3 Check what is needed. For example, the length of the entered text is 8 or more characters:

     @IBAction func myTfToCheck(_ sender: UITextField) { if let text = sender.text { let textLength = text.characters.count if textLength > 7 { //Do something print("\(text), length: \(textLength)") } } } 
    • Thanks for the great way! - Kuzin Dmitry
    • @VAndrJ the easiest way is to zayuzat already written, and not to reinvent the wheel, IMHO! - AlessandroDP
    • @AlessandroDP But why isn’t it written? The standard function of the standard UITextField . For a simple check do not need any monsters. - VAndrJ

    You can use a ready-made validator, this TextFieldValidator for obj-c, but also under the swift analogs of darkness.

    Many settings, user tips, check for password matching, and so on.

      Here is the link, there is everything you need)

      https://stackoverflow.com/questions/25223407/max-length-uitextfield

      I almost forgot, do not forget to register the delegate in the class extension - UITextFieldDelegate and specify this delegate to your TextField

       yourTextField.delegate = self 

      Take advantage.