Validate field in Objective-C

1

Scenery:

When filling in a form, it should be checked if one of the fields was filled in correctly, containing 6 numbers followed by two initials that are the Brazilian states.

It's not about email or password field, just a simple field.

How can this check be done?

Example: 123456RJ

    
asked by anonymous 21.10.2014 / 20:45

1 answer

1

Using just regular expression, you can have a method like this:

- (BOOL)validarCampo {
    NSString *string = [self.campoQualquer text];
    NSRange range = [string rangeOfString:@"^\d{6}(SP|MG|RJ)$" options:NSRegularExpressionSearch];

    if (range.location == NSNotFound) {
        return NO;
    }

    return YES;
}

It would be extended there where you complete with the rest of the acronyms of all the states.

Otherwise, for this validation of states you can have a NSArray simple and then validate out the last two characters. But this way with regular expression already works the way you need it.

    
21.10.2014 / 21:04