Domanda

I m working on a code which was implemented by someone else which is as follows

+(BOOL)emailValidate:(NSString *)email  {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
return [emailTest evaluateWithObject:email];
}

+(BOOL) IDValidate:(NSString *)ID {
NSString *regexString = @"[ A-Z0-9a-z_+-]{1,20}"; 
NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString]; 
return [regex evaluateWithObject:ID];
}

I want to alow spaces " " and commas "," in ID and email, I search a lot how to alter predicateFormat but could not find the answer.

È stato utile?

Soluzione

First thing, Coma (,) is not allowed in email. But for your scenario, Use following regular expression:

For "xyx,+ xyz125"
  ^[a-zA-Z0-9][a-zA-Z0-9,+_- ]+$
For email
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

Hopefully will solve you problem.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top