0

这是我的代码。

- (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

4 回答 4

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];

    }
于 2013-02-06T06:50:58.943 回答
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;
    }

 }

让我知道它是否有效!!!!!!!

快乐编码!!!!

于 2013-02-06T06:49:15.567 回答
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。

于 2013-02-06T06:43:36.153 回答
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;      
}
于 2013-02-06T06:44:10.997 回答