Most of the mobile applications use the email for login or other purpose. So we need to
validate email if it contains the valid email string. We can achieve it by using
NSPredicate.
1. Add following method
1. -(BOOL)isValidEmail:(NSString *)checkString
2. {
3. long numberOfAtPieces = [[checkString componentsSeparatedByString:@"@"]
count];
4.
5. if (numberOfAtPieces > 2){
6. return NO;
7. }
8.
9. BOOL stricterFilter = NO;
10. NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-
z]{2,4}";
11. NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
12. NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
13. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES
%@", emailRegex];
14. return [emailTest evaluateWithObject:checkString];
15. }
2. Call this method and pass string containing email to it like below
BOOL isValid = [Self isValidEmail:@”sample@email.com”];
If email is valid then isValid value is TRUE else value is FALSE.
If you have any further doubts, drop us an email at info@infigic.com and we’ll get back to you with the best possible solution. Infigic is a Mobile Application Development Company and we are always there to solve your queries
validate email if it contains the valid email string. We can achieve it by using
NSPredicate.
1. Add following method
1. -(BOOL)isValidEmail:(NSString *)checkString
2. {
3. long numberOfAtPieces = [[checkString componentsSeparatedByString:@"@"]
count];
4.
5. if (numberOfAtPieces > 2){
6. return NO;
7. }
8.
9. BOOL stricterFilter = NO;
10. NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-
z]{2,4}";
11. NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
12. NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
13. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES
%@", emailRegex];
14. return [emailTest evaluateWithObject:checkString];
15. }
2. Call this method and pass string containing email to it like below
BOOL isValid = [Self isValidEmail:@”sample@email.com”];
If email is valid then isValid value is TRUE else value is FALSE.
If you have any further doubts, drop us an email at info@infigic.com and we’ll get back to you with the best possible solution. Infigic is a Mobile Application Development Company and we are always there to solve your queries
Comments
Post a Comment