Text length equal to validation

A simple question arises. I got a text box that is updated and populated with the character that I want to execute when they have a length. for example, when the length is 12 (12 characters), I want her to do something and when the length is 24, etc.

Can I just do:

if (textfield.text.length == 12){ //do something } 

hope someone can help me :)

thanks

0
source share
3 answers

you should use a delegate

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 

and use if you created yourself inside it, for example

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textfield.text.length == 12){ //do something } } 
+1
source

Configure a UITextFieldDelegate and implement the textField:shouldChangeCharactersInRange:replacementString: method. Calculate which text field the replacementString will contain once, and do what you need if it matches what you are looking for.

Note that this is also called to delete and insert events.

+2
source

Write this code:

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if (textfield.text.length == 12) { condition...... } else if (textfield.text.length == 24) { condition...... } else { condition...... } } 
+1
source

Source: https://habr.com/ru/post/1415172/


All Articles