我尝试这个例子,但它有一些电子邮件类型没有检查。
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex =
@"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
@"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
@"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
@"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
@"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
@"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
@"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES[c] %@", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
此示例不检查类型为email @ domain.c-om,email @ domain.c#omc,email @ 11.160.58.260,em#il@domain.com的电子邮件地址。检查电子邮件地址的正确方法是什么?避免使用该类型?
答案 0 :(得分:1)
你从哪里得到了正则表达式?看起来你从http://www.regular-expressions.info/email.html得到它
你可以(但你不应该 - 继续阅读)用这个常规实现它 表达
然后它说
你不应该使用这个正则表达式的原因是它只检查 电子邮件地址的基本语法。 john@aol.com.nospam会的 根据RFC 5322认为是有效的电子邮件地址。
然后该页面建议使用
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
对于Objective-C,你必须转义反斜杠,你就完成了。
答案 1 :(得分:1)
分类添加方法 isEmail
-(BOOL)isEmail {
BOOL stricterFilter = YES;
NSString *stricterFilterString = @"^[_A-Za-z0-9-+]+(\\.[_A-Za-z0-9-+]+)*@[A-Za-z0-9-]+(\\. [A-Za-z0-9-]+)*(\\.[A-Za-z]{2,4})$";
NSString *laxString = @".+@([A-Za-z0-9]+\\.)+[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:self];
}
调用此方法
if([text isEmail]){
//validate email id
}else{
//not email id
}
答案 2 :(得分:0)
试试这个:
+ (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];
}
答案 3 :(得分:0)
尝试使用此代码验证给定字符串是否为电子邮件。
- (BOOL)validEmail:(NSString *) emailString
{
if([emailString length]==0)
{
return NO;
}
NSString *regExPattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger regExMatches = [regEx numberOfMatchesInString:emailString options:0 range:NSMakeRange(0, [emailString length])];
if (regExMatches == 0)
{
return NO;
}
else
{
return YES;
}
}
答案 4 :(得分:0)
+(BOOL)validEmailid:(NSString *)emailId {
@try
{
BOOL stricterFilter = NO;
NSString *stricterFilterString = @"[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,4}";
NSString *laxString = @".+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = stricterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if ([emailTest evaluateWithObject:emailId]){
return YES;
}
else{
[self showalertwithmessage:@"Invalid emailid"];
return NO;
}
}
@catch (NSException *exception)
{
NSLog(@"Error occurred in in Utils::stringIsValidEmail : %@",[exception description]);
}
}