0

In textfield I want to restrict numbers like (1234567890) and special characters but I want to allow alphanumeric characters. How I am suppose to do this?

S.J
  • 3,063
  • 3
  • 33
  • 66
  • @Merlevede Please read first, I am not asking for numbers only. I want to restrict numbers, special characters AND WANT TO ALLOW APLHA NUMERICS. – S.J Mar 05 '14 at 08:06
  • In the link I suggested there's an answer that can be adapted to do what you want. Don't expect examples to be exactly what you want. – Merlevede Mar 05 '14 at 08:09

3 Answers3

0

Use the UITextField delegate method

textField:shouldChangeCharactersInRange:replacementString:

To check the string that is about to be replaced, if you allow it then return yes if not then return no.

Here is some more information.

Apple UITextField Delegate

sbarow
  • 2,759
  • 1
  • 22
  • 37
0

try following code

+ (BOOL)isNumber:(NSString *)value {  

    if ( (value == nil) || ([@"" isEqualToString:value]) ) {  
        return NO;  
    }  

    int l = [value length];  

    BOOL b = NO;  
    for (int i = 0; i < l; i++) {  
        NSString *str =   
        [[value substringFromIndex:i]   
         substringToIndex:1];  

        const char *c =   
        [str cStringUsingEncoding:  
         NSASCIIStringEncoding];  

        if ( c == NULL ) {  
            b = NO;  
            break;  
        }  

        if ((c[0] >= 0x30) && (c[0] <= 0x39)) {  
            b = YES;  
        } else {  
            b = NO;  
            break;  
        }  
    }  

    if (b) {  
        return YES;  
    } else {  
        return NO;  
    }  
} 


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {  
    if ( (string != nil) && (string != @"") ) {  
        if (![self isNumber:string]) {  
            return NO;  
        }  
    }  
    return YES;  
}   
Teruya Kusumoto
  • 40
  • 1
  • 2
  • 7
0

You need to write a NSFormatter and assign it to your text field. Here an example implementation of a such NSFormatter which uses a NSRegularExpression to validate the NSTextField contents.

@interface XXNameElementFormatter : NSFormatter
@end

@implementation HcNameElementFormatter {
    NSRegularExpression *_re;
}

- (id)init {
    self = [super init];
    if (self) {
        [self initRegularExpression];
    }
    return self;
}

- (void)awakeFromNib
{
    [self initRegularExpression];
}

- (void)initRegularExpression
{
    NSError *reError;
    _re = [NSRegularExpression regularExpressionWithPattern:@"^[a-z]*$" options:NSRegularExpressionCaseInsensitive error:&reError];
    NSAssert(_re != nil, @"Error in regular expression, error: %@", reError);
}

- (NSString *)stringForObjectValue:(id)obj
{
    return obj;
}


- (BOOL)getObjectValue:(out __autoreleasing id *)obj forString:(NSString *)string errorDescription:(out NSString *__autoreleasing *)error
{
    *obj = string;
    return YES;
}

- (BOOL)isPartialStringValid:(NSString *__autoreleasing *)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString *__autoreleasing *)error
{
    NSParameterAssert(partialStringPtr != nil);
    NSString *partialString = *partialStringPtr;
    NSRange firstMatch = [_re rangeOfFirstMatchInString:*partialStringPtr options:0 range:NSMakeRange(0, partialString.length)];
    return firstMatch.location != NSNotFound;
}

@end
Flovdis
  • 2,945
  • 26
  • 49