Reputation: 64864
How can I forbid users to type numbers in a NSTextField ?
I'm currently using a custom NSFormatter showing a popup after the users typed the value, but how can I prevent the inserting right after typing instead ?
thanks
I've tried this and it almost work.. but if I type a number and then some letters i.e. "24jdf" the letters are still displayed (even if removed when the field looses focus).
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
//- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error
{
if ([self numberFromString:partialString] == nil)
return NO;
return YES;
}
Upvotes: 0
Views: 151
Reputation: 7272
You'll have to subclass your NSFormatter
and override the following method:
-(BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error {
NSString *string = *partialStringPtr;
NSCharacterSet *numbers = [NSCharacterSet decimalDigitCharacterSet];
return (NSNotFound == [string rangeOfCharacterFromSet:numbers].location);
}
Upvotes: 1