PDA

View Full Version : How to limit character insertion in TextField




priyank.ranka
Nov 19, 2008, 06:13 AM
Hi EveryOne,
I want to limit user from typing characters above certain limit. Suppose i want only ten character long word input from user via TextField and if user tries to input more than ten characters into the TextField the TextField cursor should not move. Any help be appreciated. Thanks for ur help.



Pring
Nov 19, 2008, 06:23 AM
Look at UITextFieldDelegate shouldChangeCharactersInRange

Return false if you don't want the characters to be entered.

jnic
Nov 19, 2008, 06:25 AM
Hi EveryOne,
I want to limit user from typing characters above certain limit. Suppose i want only ten character long word input from user via TextField and if user tries to input more than ten characters into the TextField the TextField cursor should not move. Any help be appreciated. Thanks for ur help.

Something like:

if (textField.text.length > 10) textField.text = [textField.text substringToIndex:10];

should do the trick.


EDIT: Never mind, Pring's solution is much cleaner :)