电子邮件验证无法使用iPhone

时间:2013-02-06 06:38:43

标签: iphone ios validation email-validation

这是我的代码。

- (BOOL)validateEmail:(NSString*)email
{
    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:email];
}

这是我用来验证我在文本字段中输入的电子邮件的功能。我不知道我在做什么是对还是错。我应该在任何其他功能中检查它而不是 - (BOOL)textFieldShouldEndEditing 如果是,请告诉我我必须使用的功能。如果我的正则表达式或我的验证错误帮助我得到正确的。

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    BOOL eb;
    if (textField.tag == 44) {
        eb =[self validateEmail:textField.description];
        NSLog(@" %s", eb ? "true" : "false");
        if(!eb)
        {
            UIAlertView *alertsuccess = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"Please enter correct email id"
                                                                  delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

            [alertsuccess show];
            [alertsuccess release]; 
        }
    }
    return YES;
}  

4 个答案:

答案 0 :(得分:0)

试试这个:

+(BOOL)emailValidate:(NSString *)email
{
    //Based on the string below
    //NSString *strEmailMatchstring=@"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";

    //Quick return if @ Or . not in the string
    if([email rangeOfString:@"@"].location==NSNotFound || [email rangeOfString:@"."].location==NSNotFound)
        //NSLog(@"%@",[email rangeOfString:@"@"]);
        //          NSLog(@"%@",[email rangeOfString:@"."]);
        return YES;

    //Break email address into its components
    NSString *accountName=[email substringToIndex: [email rangeOfString:@"@"].location];
    DLog(@"%@",accountName);
    email=[email substringFromIndex:[email rangeOfString:@"@"].location+1];
    DLog(@"%@",email);
    //'.' not present in substring
    if([email rangeOfString:@"."].location==NSNotFound)
        return YES;
    NSString *domainName=[email substringToIndex:[email rangeOfString:@"."].location];
    NSLog(@"%@",domainName);

    NSString *subDomain=[email substringFromIndex:[email rangeOfString:@"."].location+1];
    NSLog(@"%@",subDomain);

    //username, domainname and subdomain name should not contain the following charters below
    //filter for user name
    NSString *unWantedInUName = @" ~!@#$^&*()={}[]|;':\"<>,?/`";
    //filter for domain
    NSString *unWantedInDomain = @" ~!@#$%^&*()={}[]|;':\"<>,+?/`";
    //filter for subdomain
    NSString *unWantedInSub = @" `~!@#$%^&*()={}[]:\";'<>,?/1234567890";

    //subdomain should not be less that 2 and not greater 6
    if(!(subDomain.length>=2 && subDomain.length<=6)) 
        return YES;

    if([accountName isEqualToString:@""] 
       || [accountName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInUName]].location!=NSNotFound
       || [domainName isEqualToString:@""] 
       || [domainName rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInDomain]].location!=NSNotFound 
       || [subDomain isEqualToString:@""] 
       || [subDomain rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:unWantedInSub]].location!=NSNotFound)
        return YES;

    return NO;
}

如果电子邮件有效,则返回NO;如果电子邮件无效,则返回Yes。

答案 1 :(得分:0)

更改:

eb =[self validateEmail:textField.description];

到:

eb =[self validateEmail:textField.text];

此外,您可以使用以下提到的代码:

NSString *emailString = txt_EmailId.text;// storing the entered email in a string. 
// Regular expression to checl the email format. 
NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 

NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailReg]; 

if(([emailTest evaluateWithObject:emailString]!=YES)||[emailString isEqualToString:@""]) 
{
      alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Enter Email in abc@example.com format" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

      [alert show]; 
      [alert release];
      txt_EmailId.text = @"";
       return;      
}

答案 2 :(得分:0)

尝试以下代码......

    if([self validation])
{
   //do your stuff

}
else
{
  //display error...
}



    -(BOOL)validation
   {
    NSString *emailString = mailLbl.text; //your email text....
    NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",emailReg]; 

     if (([emailTest evaluateWithObject:emailString] != YES) || [emailString isEqualToString:@""])
{
    return NO;
}
    else 
    {
       return YES;
    }

 }

让我知道它的工作与否!!!!!!

快乐编码!!!!

答案 3 :(得分:0)

试试这段代码:

// *** Email ID Validation *** //

    NSString* emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 

    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 

    NSString *subjectString =emailtfd.text;

if ([emailTest evaluateWithObject:subjectString] != YES)
    {

        // *** Declare and Initialize the AlertView *** //

        UIAlertView *signupalert = [[UIAlertView alloc] initWithTitle:@"Enter Valid Email ID" message:@"abc@example.com format" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

        [signupalert show];

        [signupalert release];

    }