Tuesday, September 10, 2013

check that a string is numeric only with limit in UITextField.----- IOS

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    /* for backspace */
    if([string length]==0){
        return YES;
    }
   
    /*  limit to only numeric characters  */
   
    NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    for (int i = 0; i < [string length]; i++) {
        unichar c = [string characterAtIndex:i];
        if ([myCharSet characterIsMember:c]) {
            NSUInteger newLength = [textField.text length] + [string length] - range.length;
            return (newLength > 8 ) ? NO : YES;

        }
    }
   
    return NO;
}

No comments:

Post a Comment