0

I am trying to use NSPredicate to evaluate whether or not a NSString has both a first and last name (Essentially a space between two non-digit words). This code hasn't been working for me (Code taken & modified slightly from: What are best practices for validating email addresses in Objective-C for iOS 2.0?:

-(BOOL) validName:(NSString*) nameString {
    NSString *regExPattern = @"[A-Z]+_[A-Z]";
    NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSUInteger regExMatches = [regEx numberOfMatchesInString:nameString options:0 range:NSMakeRange(0, [nameString length])];


    if (regExMatches == 0) {
        return NO;
    } else
        return YES;
    }
}

I think there is something wrong with my regEx pattern, but I'm not sure how to fix it. This is how I check the string:

if([self validName:nameTextField.text]) {
  // Valid Name
} else {
  // Name no valid
}
Community
  • 1
  • 1
iMinichrispy
  • 134
  • 2
  • 14

2 Answers2

1

First, if you want to match a space, then just put a space in the regex pattern. The underscore you have now will require an underscore in your name field in order to match.

Second, NSPredicate matches the whole string against the regex, so the pattern would not catch normal last names (which have more than one character), even with the space. You'll need to add some expression that covers the last part of the name.

Third, since you pass the text field directly into the check, you are putting some pressure on your users to type everything like you expected. You might want to clean the string a bit first, before testing. Personally, I would at least trim the string for spaces and replace multiple spaces with a single one.

Here is some code that does this:

NSString *regExPattern = @"[A-Z]+ [A-Z]+"; //Added a "+" to match the whole string up to the end.

Check:

NSString *name = nameTextField.text;


name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

name = [name stringByReplacingOccurrencesOfString:@" +" 
                                       withString:@" " 
                                          options:NSRegularExpressionSearch 
                                            range:NSMakeRange(0, name.length)];
if([self validName: name]) {
  // Valid Name
} else {
  // Name no valid
}

As you can imagine there are many ways to do this, but this is a start. You should consider your test for "correct" names, though, as there are many names that won't pass you simple regex, for instance names with apostrophes and accents, for instance:

  1. Jim O'Malley
  2. Zoë Jones

etc.

Monolo
  • 18,205
  • 17
  • 69
  • 103
  • This is what I was looking for, thank you. I have a list of the possible names that will be entered, but just in case the list changes, how could I add support for apostrophes and special characters – iMinichrispy Jul 12 '12 at 17:30
1

If you just want to check for the space-separated fore- and surname, I would try this:

- (BOOL)validName:(NSString*)name
{
    NSArray *components = [name componentsSeparatedByString:@" "];
    return ([components count] >= 1);    
}

This will check if you've at least two components separated by a space. This will also work for names with 3 or more components (middle names).

cweinberger
  • 3,518
  • 1
  • 16
  • 31