Validate Objective-C field

Asked

Viewed 238 times

1

Setting:

When filling out a form, it must be checked if one of the fields has been filled in correctly, containing 6 numbers followed by two initials that are the states of Brazil.

This is not email and password field, just a simple field.

How can this check be done ?

Example: 123456RJ

1 answer

1


Using only 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 it with the rest of the acronyms of all states.

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

  • I’m gonna take a test!!

  • Works perfectly!! Thank you!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.