Is there any way to prevent consecutive "."
in email address using NSPredicate
? I can check it other way that whether current and previous input is ".". But I already use NSPredicate
. Thus, if I just have to include some condition in that only then I prefer this.
My code :
NSString * email = self.emailTextField.text;
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailValidation =[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
BOOL isValidEmail = [emailValidation evaluateWithObject:email];
Thank you.
You can try modifying the middle part of your expression to use (?!\\.)
negative lookahead for matches of the dot .
character, like this:
[A-Z0-9a-z._%+-]+@([A-Za-z0-9-]|\\.(?!\\.))+\\.[A-Za-z]{2,4}
The \\.(?!\\.)
anywhere in an expression says "match a single dot only when the symbol that follows is not a dot". The symbol that follows does not get consumed in the process of checking the lookahead. If you want to prevent dots in the e-mail's initial part as well, modify the part before @
in the same way as the middle part:
([A-Z0-9a-z_%+-]|\\.(?!\\.))+@([A-Za-z0-9-]|\\.(?!\\.))+\\.[A-Za-z]{2,4}