1

可能重复:
iPhone sdk 中 textField 上的电子邮件验证

我有一个应用程序,我想在其中获取用户的电子邮件地址作为输入。我想检查用户是否正确输入了电子邮件地址;像 @ 应该在字段中的文本中。我怎样才能做到这一点?

textField.text=@"ali@hotmail.com"

如果文本框有这个值,那么确定,否则它应该提醒电子邮件地址应该是正确的

4

4 回答 4

5

您可以使用 regExp 验证,例如这个函数

-(BOOL) isValidEmail:(NSString *)checkString
{
    checkString = [checkString lowercaseString];
    BOOL stricterFilter = YES; 
    NSString *stricterFilterString = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSString *laxString = @".+@.+\\.[A-Za-z]{2}[A-Za-z]*";

    NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    return [emailTest evaluateWithObject:checkString];
}

像这样使用它

if([self isValidEmail:textField.text])
   //Valid
else
   //Not valid, do the alert
于 2012-06-26T06:25:40.390 回答
3

您可以使用以下方法:-

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

TRUE如果输入格式正确,则该方法将返回,否则FALSE

于 2012-06-26T06:26:17.473 回答
0
-(BOOL)isValidEmaill:(NSString*)in_EmailId
{
    if( (0 != [in_EmailId rangeOfString:@"@"].length) &&  (0 != [in_EmailId rangeOfString:@"."].length) )  
    {  
        NSMutableCharacterSet *invalidCharSet = [[[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy]autorelease];  
        [invalidCharSet removeCharactersInString:@"_-"];  

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

        // If username part contains any character other than "."  "_" "-"  
        NSString *usernamePart = [in_EmailId 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 = [in_EmailId 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  
    {
        //[mEmailAddrTextField becomeFirstResponder];
        return NO; 
    }
}
于 2012-06-26T06:27:00.810 回答
0

试试这个

NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx]; 
    if  ([emailTest evaluateWithObject:m_CtrlTxtFld_Email.text] != YES && [m_CtrlTxtFld_Email.text length]!=0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please enter valid email address" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }
于 2012-06-26T06:28:54.063 回答