I have three types of text fields. in that I need only minimum numbers only . below code I write-in but its not working. help me
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (country.text.length <= 4) {
return YES;
}
if(code.text.length<=4 ) {
return YES;
}
if(textPhone.text.length<=10) {
return YES;
}
return YES;
}
Your logic is flawed here. You don't even base your checks on which field is edited.
Try this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// Allow backspace anyway
if (range.length == 0)
return YES;
if (textField == country)
return (country.text.length <= 4);
else if (textField == code)
return (code.text.length <= 4);
else if (textField == textPhone)
return (textPhone.text.length <= 10);
// Default for all other fields
return YES;
}