Restrict textField input to numeric values only

Hi - I have been trying to write a script that limits textfield entries to numeric values only and to a maximum of five. I tried using NSCharacterSet’s characterSetWithCharactersInString_(“0123456789”) and then looping through the textfield’s strings, but id did not work. I then thought of getting notifications from the textfield and getting the numeric values using currentEditor() and then textStorage()'s string(), since to my understanding that a textfield is nothing but an NSTextView, but i am unable to put any meaningful code to get any results.

Could someone assist me in writing the code please.

Regards,
t

How about you just add a number formatter on the field? You can’t tab out if the entry isn’t a number. Not sure how you deal with using a button if there are no other fields to enter.

I did use an NSNumberFormatter, but it still lets me enter strings other that numerics. I failed to mention in my post that I an also trying to get the process to stop as soon as a non-numeric value was entered.

I did find controlTextDidEndEditing method from NSControl which has NSNotifications as a parameter.

  • (void)controlTextDidEndEditing:(NSNotification *)aNotification

I hope i can get some results out of this.

t.

Hi,

take a look at this article.
It describes how to subclass NSNumberFormatter

Thank you Stefan.

t.

I tweaked the formatter code on the mentioned site a bit
to be able to move the insertion location and delete characters

.h file


@protocol SKNumberOnlyFormatterDelegate <NSObject>

@optional // delegate is not required
- (void)maxNumberOfCharsReached;
@end

#import <Foundation/Foundation.h>

@interface SKNumberOnlyFormatter : NSNumberFormatter

@property (assign) id <SKNumberOnlyFormatterDelegate>delegate;

@end

.m file


static NSUInteger maxLength = 5;

#import "SKNumberOnlyFormatter.h"

@implementation SKNumberOnlyFormatter

- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
      proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
             originalString:(NSString *)origString
      originalSelectedRange:(NSRange)origSelRange
           errorDescription:(NSString **)error {
    
       BOOL returnValue = YES;
    
    // empty strings are ok
    if ([*partialStringPtr length] == 0) return YES;
    
    NSCharacterSet *decimalDigitCharacterSet = [NSCharacterSet decimalDigitCharacterSet];
    
    // only allow numbers in the fields
    NSRange proposedSelRange = *proposedSelRangePtr;
    NSRange insertionLocation = NSMakeRange(proposedSelRange.location - 1, 1);
    NSString* currentLetter = [*partialStringPtr substringWithRange:insertionLocation];
    
    if (proposedSelRange.location == 0)
        returnValue = YES;
    else
        returnValue = [currentLetter rangeOfCharacterFromSet:decimalDigitCharacterSet].location != NSNotFound;
    
    // if length is equal to max length don't allow any other chars
    if ([*partialStringPtr length] > maxLength) {
        if ([self.delegate respondsToSelector:@selector(maxNumberOfCharsReached)]) {
            [self.delegate maxNumberOfCharsReached];
        }
        returnValue = NO;
    }
    return returnValue;
}

@end

Thank you again Stefan.

Regards,
t.