Frage

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.

War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top