Potwierdź adres e-mail w UITextField w iphone [duplikat]

Możliwy duplikat:
upewnij się, że użytkownik wprowadził ciąg adresu e-mail w prawidłowym formacie?

Mam UITextField w którym biorę adres e-mail od użytkownika, który wprowadzają, i chcę potwierdzić ten adres e-mail, na przykład chciałbym, aby sprawdzić, czy zawiera symbole takie jak znak @ i inne znaki e-mail.

Jeśli w adresie e-mail wystąpił błąd, powinien on pokazać UIAlertView z napisem "Wprowadź poprawny adres e-mail".

Author: Community, 2012-09-04

3 answers

Objective C Style

NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];

if ([emailTest evaluateWithObject:email.text] == NO) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test!" message:@"Please Enter Valid Email Address." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

    return;
}

Styl Swift

class func isValidEmail(emailString:String) -> Bool {

    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,10}"
    var emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)

    let result = emailTest?.evaluateWithObject(emailString)
    return result!
}
 22
Author: Sameera Chathuranga,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-06-05 10:52:31

Możesz to zrobić używając NSPredicate

//suppose emailID is your entered email address NSString
NSString *emailFormat1 = @"[A-Z0-9a-z._]+@[A-Za-z0-9]+\\.[A-Za-z]{2,4}";     


NSPredicate *emailTest1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailFormat1]; 


if ([emailTest1 evaluateWithObject:emailID]||[emailTest2 evaluateWithObject:emailID]) {
   //yes it is valid
}
else
    //no it is invalid
 3
Author: Neo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-04 08:29:04
 1
Author: iOS,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:07:19