Email validation not working in some test cases in iOS

228 views Asked by At

I want to validate the email in iOS. I've written a category class and here's my code but in some cases like [email protected] it's not working...

If the user type two times .com or .in continuously it is not detecting it...

I tried with some solutions but that also not working. That's why am asking here

- (BOOL)isValidEmail {
   NSString *emailString = [self stringByTrimmingCharactersInSet:   [NSCharacterSet whitespaceAndNewlineCharacterSet]];

   BOOL isValid = YES;
   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];

   if (![emailTest evaluateWithObject:emailString]) {
      isValid = NO;
   }

   return isValid;

}

5

There are 5 answers

0
Alessio Campanelli On

this regular expression works correctly:

- (void) checkMail{
   NSString *emailRegex = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,}$";
   NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
   if (self.mail.text.length <= 0) {
    //error empty
}else if(![emailTest evaluateWithObject:self.mail.text]){
    //error address inserted...
}else{
    //correct email!
};

}

0
Keyur Hirani On

Check it.

-(BOOL) isValidEmail:(NSString *)email
    {
       BOOL filter = NO; 
       NSString *stricterString = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
       NSString *laxString = @"^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
       NSString *emailRegex = filter ? stricterString : laxString;
       NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
       return [emailTest evaluateWithObject:email];
    }
0
Ankit Saini On

Following function worked for me like a charm:

func validateEmailWithString(email: String) -> Bool {
    let emailRegex: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
    let emailTest: NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex)
    return emailTest.evaluateWithObject(email)
}
0
Dishant Rajput On

TRY THIS

if (![self validateEmailWithString:txtEmail.text])

{
//@"Please Enter a Valid Email Address"];
}




- (BOOL)validateEmailWithString:(NSString*)checkString
{
BOOL stricterFilter = NO;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:checkString];
}
0
Yogesh Raut On

Please check following Answer. It may help you.

-(BOOL) NSStringIsValidEmail:(NSString *)checkString
    {
        BOOL stricterFilter = NO; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
        NSString *stricterFilterString = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}$";
        NSString *laxString = @"^.+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*$";
        NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

        NSArray *firstArray = [checkString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"@"]];
        NSString *input = [firstArray objectAtIndex:1];

        NSSet *aSet = [NSSet setWithArray:[input componentsSeparatedByString:@"."]];
        NSString *result = [aSet.allObjects componentsJoinedByString:@"."];

        if ([input isEqualToString:result]) {
            BOOL checkValue = [emailTest evaluateWithObject:checkString];
            return checkValue;
        }else{

            return FALSE;
        }


    }