A better function which handles backspaces correctly and limits the characters to the supplied length limit is the following:
#define MAXLENGTH 8
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
int length = [textField.text length] ;
if (length >= MAXLENGTH && ![string isEqualToString:@""]) {
textField.text = [textField.text substringToIndex:MAXLENGTH];
return NO;
}
return YES;
}
Cheers!