电子邮件ID验证

时间:2011-06-01 13:41:31

标签: iphone

我制作了一个iphone应用程序,其中我使用了电子邮件ID验证。我使用的代码如下:

NSString *valid = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}"; 
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", valid];

if ([emailTest evaluateWithObject:Id.text] == NO) 
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Validation" message:@"Email Id is not Valid" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];

    }
    if ([emailTest evaluateWithObject:Id.text] == YES) 
    {
    NSString *b = Id.text; 
    NSLog(@"value of the Id is : %@",b);

        if([b length] != 0)
        {
            MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;

            //[picker setSubject:@"Your iPhone fan"];//@"Hello iPhone!"
            [picker setToRecipients:[NSArray arrayWithObject:b]];

            NSString *emailBody =@""; //@"";//@"Nice  to See you!";
            [picker setMessageBody:emailBody isHTML:NO];

            [self presentModalViewController:picker animated:YES];
            [picker release];

        }
    }

但是如果我在文本字段中放入1@gmail.com,那么它仍会显示有效的ID,但实际上它是无效的电子邮件ID。

4 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:1)

-(IBAction)idValidator:(id)sender {
NSString *str=idField.text;

NSString *regex =@"\\b([a-zA-Z0-9%_.+\\-]+)\\b"; 

NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

BOOL x= [regextest evaluateWithObject:str];

if (x==FALSE) 
{
    //do something

}
else {
    //do something
}   
}   

答案 2 :(得分:0)

你只是通过字符串和返回有效的电子邮件

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

答案 3 :(得分:0)

//For validating email id use following code ....

-(BOOL)validateEmail:(NSString*)email
{

    if( (0 != [email rangeOfString:@"@"].length) &&  (0 != [email rangeOfString:@"."].length) )
    {
        NSMutableCharacterSet *invalidCharSet = [[[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy]autorelease];

        [invalidCharSet removeCharactersInString:@"_-"];

        NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch];

        // If username part contains any character other than "."  "_" "-"
        NSString *usernamePart = [email substringToIndex:range1.location];

        NSArray *stringsArray1 = [usernamePart componentsSeparatedByString:@"."];
        for (NSString *string in stringsArray1) {
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet: invalidCharSet];
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
                return NO;
        }
        NSString *domainPart = [email substringFromIndex:range1.location+1];
        NSArray *stringsArray2 = [domainPart componentsSeparatedByString:@"."];
        for (NSString *string in stringsArray2) {
            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:invalidCharSet];
            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
                return NO;
        }
        return YES;
    }
    else // no '@' or '.' present
        return NO;
}

//For validating URL use following code ....

- (BOOL) validateUrl: (NSString *) candidate 
{
    NSString *urlRegEx =  @"((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";

  NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx]; 

    if ([urlTest evaluateWithObject:candidate]) 
        return YES;
    else
        return NO;
}