简体   繁体   中英

Validate textfield using Reactive Cocoa swift

I am trying to validate UITextField if it has greater than 3 characters it should return true else return false. I tried using the below code but it is not working. What am I doing wrong?

 let validUserNameSignal = self.nameTextField.reactive.trigger(for: .valueChanged).observeValues {
        value in


    }.map { (value) in

        String(describing: value).characters.count > 3 ? true:false

    }
    print("user name valid result is \(validUserNameSignal)")

Here's how the code should look.

let validUserNameSignal =
        self.nameTextField
            .reactive
            .continuousTextValues
            .skipNil()
            .map { $0.characters.count > 3 }

validUserNameSignal.observeValues { value in
    print("user name valid result is \(value)")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM