【问题标题】:Swift validate 2 uitextfield password during text entrySwift 在文本输入期间验证 2 uitextfield 密码
【发布时间】:2020-11-23 05:15:27
【问题描述】:

我有 2 个文本字段,其中包含 1 个输入 pin 和 1 个重新输入 pin,我尝试使用 shouldChangeCharactersIn 进行验证,因此如果 pin 不同,它会自动显示一条小文本消息,表示 pin 不同。很好,但不是很好,因为如果我只是在我的输入引脚中输入 6 引脚,并且我还在重新输入引脚中输入 6 引脚,(在这种情况下,我故意输入错误的引脚)并且消息不会弹出,但是如果我输入超过 6 次,比方说输入 7 的数字会显示消息,为什么我不明白。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    

    if textConfirmPIN.text!.count == 6 && textPIN.text!.count == 6 {
        if textConfirmPIN.text != textPIN.text{
            self.showError(message: "PIN is not the same")
        }
    }

    return true
}

抱歉我的英语不好,但谁能告诉我发生了什么事?谢谢大家

【问题讨论】:

    标签: swift validation passwords textfield


    【解决方案1】:

    使用文本字段编辑更改事件而不是 shouldChangeCharactersIn,因为当您在 shouldChangeCharactersIn 中检查 textConfirmPIN.text!.count 时,最后输入的字符仍然不存在,直到返回 true

    这就是你输入第 7 个字符时调用它的原因

    【讨论】:

    • 啊,好吧2现在我明白了,谢谢先生的回答,我花了一整天的时间来解决这个问题
    猜你喜欢
    • 2014-02-12
    • 2016-10-18
    • 1970-01-01
    • 2015-10-08
    • 2013-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    相关资源
    最近更新 更多