如何在UITextfield中检查该电子邮件地址是否已完美输入

时间:2012-06-26 06:23:33

标签: iphone xcode

  

可能重复:
  Email validation on textField in iPhone sdk

我有一个应用,我希望将用户的电子邮件地址作为输入。我想检查用户是否正确输入了电子邮件地址;喜欢@应该在该字段的文本中。我怎么能这样做?

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

如果文本框中包含此值,则表示正常,否则应警告该电子邮件地址应该正确

4 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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; 
    }
}

答案 3 :(得分: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;
    }