iosswiftreactive-cocoareactive-cocoa-5

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)")

Solution

  • 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)")
    }