Can someone help me figure out an easy way to limit the UITextField range, so that the entered number is between 14 and 70, and if the number is greater or less that the one inside the range, the textfield would just clear itself? I have already limited the length of the entered text to be not more than 2 characters like this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newString = [ageField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] > 2);
The only thing left is to implement this behavior. Anyone?
UPDATE:
NSInteger v = [newString integerValue];
if (v < 14 || v > 72) {
// Do whatever you do when the string value is invalid
// BUT ALSO ADD:
textView.text = @"";
}
Instead of returning try:
if (newString.length != 2) {
// do what you want!
return;
} else {
// PUT YOUR CODE HERE
}