How to check the target of the first name c?

Hi everyone, I'm trying to check the name in objective-c. Please note that the name can contain spaces and only a letter.

Here is my code that does not work:

NSString *nameRegex = @"^[A-Z][a-z]*[\\s{L}\\s{M}\\s{Nl}][A-Z][a-z]*$";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex];
BOOL isValid = [nameTest evaluateWithObject:name];
return isValid;
+4
source share
4 answers

I understood:

NSString *emailRegex = @"[a-zA-z]+([ '-][a-zA-Z]+)*$";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:name];
return isValid;
+5
source

This method works for me:

-(BOOL)validate:(NSString *)string
{
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-Z ]" options:0 error:&error];    
    NSUInteger numberOfMatches = [regex numberOfMatchesInString:string options:0 range:NSMakeRange(0, [string length])];        
    return numberOfMatches == string.length;
}
0
source
BOOL check=NO;
for(int i=0;i<[txt.text length];i++)
{
if([txt.text characterAtIndex:i]<=64 && [txt.text characterAtIndex:i]>=33)
{
    check=YES;

}else if([txt.text characterAtIndex:i]<=122 && [txt.text characterAtIndex:i]>=65)
{
    if([txt.text characterAtIndex:i]<=96 && [txt.text characterAtIndex:i]>=91)
    {
        check=YES;

    }

}
}
if(check==YES)
{
    [self alert:@"Name Contain spetial character"];
    return NO;

}else
{
    [self alert:@"Name Inserted succesfully"];

    return YES;
}
return  NO;
0
source

I am using the following code

NSString *yourstring = @"hello";

NSString *Regex = @"[a-zA-Z][a-zA-Z ]*";
NSPredicate *TestResult = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",Regex];

if ([TestResult evaluateWithObject:yourstring] == true)
{

    // validation passed
}
else
{
// invalid name
}
0
source

All Articles