How to check if UITextField's text is valid email?

后端 未结 5 1138
清酒与你
清酒与你 2020-12-28 23:45

I have a view controller with 3 UITextFields (username, email, and password).

I need a method that checks first, if all fields have text in them, then check if the e

5条回答
  •  无人及你
    2020-12-28 23:56

    I have used Mimit's solution but modified the emailRegex to allow for longer names such as museum. So the last curly brackets now says {2, 6} not {2, 4}. And I tested it with the longer name and it works. Thanks Mimit for the easy solution.

    -(BOOL) validateEmail: (NSString *) candidate {
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];    //  return 0;
       return [emailTest evaluateWithObject:candidate];
    }
    

提交回复
热议问题