Textfield validation rxswift iOS || Textfield restrict the length using rxswift

 Problem:

I want to validate my textfield to restrict that to enter particular length of characters only.Not to exceed that limit.How can i achieve that?


Solution:

You have to add one extension function for your textfield with sending limits and disposebag to that textfield extension and restrict like below.

func extension UITextField {

    func validate(limit:Int,dispose:DisposeBag) {

        self.rx.text.orEmpty

        .scan("") { (previous, new) -> String in

            if new.count >limit {

                return previous ?? String(new.prefix(40))

            } else {

                return new

            }

        }

        .subscribe(self.rx.text)

        .disposed(by: dispose)


    }

}


You can call the extension function as like below.

        cell.TxtView.validate(limitlimit,disposecell.disposeBag)


Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift