Ensure User has entered email address string in correct format?

后端 未结 3 1377
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 00:10

i have a text field in Contact screen and the user need to enter email address to send me message. Whats the best way to ensure the user has entered a valid email address su

3条回答
  •  再見小時候
    2021-01-07 00:15

    Try the following:

    - (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 0;
        return [emailTest evaluateWithObject:candidate];
    }
    
    
    -(IBAction)btnTapped:(id)sender{
    
        if([self validateEmail:[txtEmail text]] ==1)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Correct Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
    
        }
        else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Incoorect Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
            [alert show];
            [alert release];
        }
    }
    

提交回复
热议问题