1

I am trying to validate email using reg-ex. here is the code...

+ (BOOL) stringIsValidEmail:(NSString *)checkString;
{
    NSString *emailRegEx =
    @"(?:[a-zA-Z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
    @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
    @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
    @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
    @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
    @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
    @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; 

    return [emailTest evaluateWithObject:checkString];
}

now I don't have much knowledge about regex but this accepts a@a.c as a valid email. But this should not be the case and at least two characters should be required at the end. What paramater do I need to change in this so it returns false. I have hit and tried but that didn't work. Thanks for your help.

Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
  • I believe you can set number of characters criteria with something like {2} or {3}. Explore in that direction. – Bourne Oct 09 '12 at 13:07
  • 1
    Do not do that and see: [Validating emails with regex][1] [1]: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address – Rikkles Oct 09 '12 at 13:12
  • As Rikkles's link highlights, it's not really possible to completely validate an email address with a regex. It *might* be useful in some cases to use a regex for a simple/crude validation. But that all depends on what you are trying to do. What are you trying to do? – dan1111 Oct 09 '12 at 13:23
  • @dan1111 i am validating this on iPhone so that server side validation runs can be minimised. – Ankit Srivastava Oct 09 '12 at 13:29

2 Answers2

1

Too much symbols, you can try this

- (BOOL) IsValidEmail:(NSString *)checkString {
    BOOL sticterFilter = YES; 
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";
    NSString *emailRegex = sticterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:checkString];
}
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
NeverBe
  • 5,213
  • 2
  • 25
  • 39
  • You can write it simpler, instead of `[A-Za-z]{2}[A-Za-z]*`, just write `[A-Za-z]{2,}`. `{2,} means match 2 or more.` – stema Oct 09 '12 at 13:10
1

Try using this as regex

NSString *emailRegex = @"[A-Z0-9a-z._]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 

the {2,4} especially validates that the ending characters should be alphabets and more than two in count

AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
  • 1
    That doesn't allow IDNA. You should allow dashed and numbers in the TLD also. Better yet, just check that there is an @ sign (and a dot somewhere after it, if you must) or even better, abandon this doomed endeavor. – tripleee Oct 09 '12 at 13:18