`textField:shouldChangeCharactersInRange:`を使用して、文字が一致していないことをどのように見つけますか?

ラフル・アナンド

15文字を超える長さを入力してはならないため、テキストフィールドの検証を設定するために以下のコードを使用しています。

 let limitLength = 15

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

    // set maximum length for password and confirm password
    if textField == txtPassword { // if textfield password is editing

        guard let text = textField.text else { return true }
        let newLength = text.characters.count + string.characters.count - range.length
        return newLength <= limitLength

    } else if textField == txtConfirmpassword { // if textfield confirm password is editing
        guard let text = textField.text else { return true }
        let newLength = text.characters.count + string.characters.count - range.length
        return newLength <= limitLength
    }
    return true
}

パスワードと確認パスワードとして2つのテキストフィールドがあり、それらが同じ文字列を持っているかどうかの検証を確認したいですか?

「textField:shouldChangeCharactersInRange」は使用する必要のあるメソッドですが、ユーザーが入力しているときに比較する方法がわからないため、同じではないというアラートメッセージを表示する必要があります。注:-ボタンをクリックしても比較しないでください。

Andey Satyanarayana

このようにしてみてください

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

// set maximum length for password and confirm password
if textField == txtPassword { // if textfield password is editing

    guard let text = textField.text else { return true }
    let newLength = text.characters.count + string.characters.count
    return newLength <= limitLength

} else if textField == txtConfirmpassword { // if textfield confirm password is editing
    guard let text = textField.text else { return true }
    let newLength = text.characters.count + string.characters.count
    if newLength <= limitLength && txtPassword.text.hasPrefix("\(text)\(string)") {
    return true
} else {
   // here u can show alert
     textField.resignFirstResponder()
     return false
 }
      }
        return true
   }

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ