Create a subclass ofNSNumberFormatter
, add this method, and hook up the formatter to your text fields.- (BOOL) isPartialStringValid: (NSString **) partialStringPtr proposedSelectedRange: (NSRangePointer) proposedSelRangePtr originalString: (NSString *) origString originalSelectedRange: (NSRange) origSelRange errorDescription: (NSString **) error { NSCharacterSet *nonDigits; NSRange newStuff; NSString *newStuffString; nonDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; newStuff = NSMakeRange(origSelRange.location, proposedSelRangePtr->location - origSelRange.location); newStuffString = [*partialStringPtr substringWithRange: newStuff]; if ([newStuffString rangeOfCharacterFromSet: nonDigits options: NSLiteralSearch].location != NSNotFound) { *error = @"Input is not an integer"; return (NO); } else { *error = nil; return (YES); } } // isPartialStringValid