我正在检查我的应用程序中的电子邮件验证,但这没有发生,我正在使用Xcode 8



我正在检查电子邮件的验证,但以下是我的代码。 请告诉我它背后的问题。

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


-(BOOL)RagistationValidation{
    if ([_txtmail.text isEqualToString:@""] ) {
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Wrong Email Id" message:@"Please enter Emailid" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];
    }else if ([self validateEmail:_txtmail.text]){
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Wrong Email Id" message:@"Please enter valid Emailid" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];

    }

请帮助我。

您的正则是这样的,所以请更换以进行电子邮件验证

 +(BOOL)isValidEmailID:(NSString *)email
    {
        NSString *regExPattern = @"[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9][A-Za-z0-9]+(\.[A-Za-z0-9][A-Za-z0-9]+)*(\.[a-zA-Z]{2,4})+";
        NSPredicate * emailValidator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regExPattern];
        BOOL isValid = [emailValidator evaluateWithObject:email];
        return isValid;
    }

用于检查

if(![self isValidEmailID:self.txt_username.text]){

最新更新