Ios UITextfield中仅用于名称的正则表达式

Ios UITextfield中仅用于名称的正则表达式,ios,regex,xcode,uitextfield,Ios,Regex,Xcode,Uitextfield,我为只包含字母的名称创建以下正则表达式代码 For Eg: John -Correct John123-Incorrect John 123 - Incorrect John Micheal -it shows incorrect as its have whitespace . How to fix it? -(BOOL)validateNameWithString:(NSString*)nameStr { NSString *nameRegex = @"^[a

我为只包含字母的名称创建以下正则表达式代码

For Eg:
John -Correct
John123-Incorrect
John 123 - Incorrect
John Micheal -it shows incorrect as its have whitespace . How to fix it?



 -(BOOL)validateNameWithString:(NSString*)nameStr
    {
        NSString *nameRegex = @"^[a-zA-Z]*$";

        NSPredicate *testRegex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
        if(![testRegex evaluateWithObject:nameStr])
            return NO;
        else
            return YES;

    }

只需将空白标记
\s
添加到允许的标记列表中:

^[a-zA-Z\s]*$
检查这个

正如评论中指出的,你需要避开反斜杠

NSString *nameRegex =@"^[a-zA-Z\\s]*$";

未知转义序列\s warningNSString*nameRegex=@“^[a-zA-Z\\s]*$”;修好它谢谢