Eric
Eric

Reputation: 13

UITextField must have minimum characters to activate UIButton

`I am new to Xcode/Swift, and am unable to figure out how to make a UITextField require a minimum number of characters (e.g.- 7) before a UIButton is made active (e.g.- Next).

If the user does not enter at least 7 characters into the UITextField (e.g.- password field), the UIButton (e.g.- "Next") remains inactive. However, when the user does enter 7 or more characters into the UITextField, the UIButton is activated and works as normal (clicks through to the next screen).

Below is my View Controller code. The UITextField in question is "passwordField" and the UIButton is "toViewController3":

import UIKit

class ViewController2: UIViewController, UITextFieldDelegate {

@IBOutlet weak var passwordField: UITextField!

@IBAction func toViewController3(_ sender: Any) {
    print("button pressed")
    self.performSegue(withIdentifier: "ViewController2ToViewController3Segue", sender: self)
}

@IBAction func backToViewController1(_ sender: Any) {
    print("back button pressed")
    self.performSegue(withIdentifier: "ViewController2ToViewController1Segue", sender: self)

}

override func viewDidLoad() {
    super.viewDidLoad()
    print("ViewController2 has loaded")

    passwordField.delegate = self
    passwordField.becomeFirstResponder()

}

// Hide Keyboard when touch on screen
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
    print("Keyboard Hidden by Screen Tap")
}

// Hide keyboard when Return key is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    passwordField.resignFirstResponder()
    print("Keyboard Hidden by Return Key")
    return true
}

}

}

Upvotes: 1

Views: 854

Answers (2)

Robert Dresler
Robert Dresler

Reputation: 11140

Using target for .editingChanged

Create action which gets called when user types or deletes character from TextField (control event .editingChanged). Then set button's isEnabled property depending on if TextField's text has at least 7 characters.

...
textField.addTarget(
    self, 
    action: #selector(textFieldChangedValue(_:)), 
    for: .editingChanged
)
...

@IBAction func textFieldChangedValue(_ sender: UITextField) {
    yourButton.isEnabled = sender.text!.count >= 7
}

Using RxSwift library

If you are familiar with using RxSwift library, you can simply observe rx.text.orEmpty control property of UITextField, in map you can validate the text and then you can bind it to button's rx.isEnabled binder.

textField.rx.text.orEmpty
    .map { $0.count >= 7 }
    .bind(to: yourButton.rx.isEnabled)
    .disposed(by: bag)

Upvotes: 4

Mahdi
Mahdi

Reputation: 29

let text = UITextField()
text.addTarget(self, action: #selector(textFieldChanged(_:)), for: .valueChanged)

@objc func textFieldChanged(_ textField: UITextField) {
    if textField.text?.count > 7 {
        // active button
    } else {
        // disable button
    }
}

Upvotes: 0

Related Questions