Home > database >  how to check textfield is not empty and button enable
how to check textfield is not empty and button enable

Time:11-12

I want to check textfield is empty

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let text = (textField.text! as NSString).replacingCharacters(in: range, with: string)
        if !text.isEmpty {
            self.completeRegisterBtn.isEnabled = true
        } else {
            self.completeRegisterBtn.isEnabled = false
        }
        
        return true
    }

This is my code but it is only check input new text. I have already input textfields by data. How can i check?

Furthermore not only want to check textfield but also a button(from 2 button) is selected check

Please help me. Thank you

CodePudding user response:

I have attached the code how to check that textFiled is empty or not

let txtField = UITextField()
txtField.text = "testing"
if txtField.text != "" {
    btn_Pause.isEnabled = true
} else {
    btn_Pause.isEnabled = false
}

-> Using isEmpty function

if txtField.text?.isEmpty == true {
    btn_Pause.isEnabled = false
} else {
    btn_Pause.isEnabled = true
}

-> Using character count

if txtField.text?.count ?? 0 > 0 {
    btn_Pause.isEnabled = true
} else {
    btn_Pause.isEnabled = false
}

CodePudding user response:

Answer as per your comment:

if textField.text != "" && btn.isSelected {
            
 }
  • Related