Tuesday 6 August 2013

Check that string contains only numeric values (number 0-9).


+(BOOL) validateStringContainsNumbersOnly:(NSString*)strng
{
    NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];

    s = [s invertedSet];
    //And you can then use a string method to find if your string contains anything in the inverted set:

    NSRange r = [strng rangeOfCharacterFromSet:s];
    if (r.location != NSNotFound) {
        NSLog(@"the string contains illegal characters");
        return NO;
    }
    else
        return YES;
}

No comments:

Post a Comment