我想验证iOS中的电子邮件。我已经编写了一个类别类,这里是我的代码,但在某些情况下,例如 abc@gmail.com.com 它没有工作......
如果用户连续两次输入.com或.in,则表示没有检测到它......
我尝试了一些解决方案,但也没有用。这就是我在这里问的原因
- (BOOL)isValidEmail {
NSString *emailString = [self stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
BOOL isValid = YES;
BOOL sticterFilter = 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 = sticterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if (![emailTest evaluateWithObject:emailString]) {
isValid = NO;
}
return isValid;
}
答案 0 :(得分:0)
检查一下。
-(BOOL) isValidEmail:(NSString *)email
{
BOOL filter = NO;
NSString *stricterString = @"^[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 = filter ? stricterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:email];
}
答案 1 :(得分:0)
这个正则表达式可以正常工作:
- (void) checkMail{
NSString *emailRegex = @"^[A-Z0-9a-z\\._%+-]+@([A-Za-z0-9-]+\\.)+[A-Za-z]{2,}$";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if (self.mail.text.length <= 0) {
//error empty
}else if(![emailTest evaluateWithObject:self.mail.text]){
//error address inserted...
}else{
//correct email!
};
}
答案 2 :(得分:0)
请检查以下答案。它可能对你有帮助。
-(BOOL) NSStringIsValidEmail:(NSString *)checkString
{
BOOL stricterFilter = NO; // Discussion http://blog.logichigh.com/2010/09/02/validating-an-e-mail-address/
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];
NSArray *firstArray = [checkString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"@"]];
NSString *input = [firstArray objectAtIndex:1];
NSSet *aSet = [NSSet setWithArray:[input componentsSeparatedByString:@"."]];
NSString *result = [aSet.allObjects componentsJoinedByString:@"."];
if ([input isEqualToString:result]) {
BOOL checkValue = [emailTest evaluateWithObject:checkString];
return checkValue;
}else{
return FALSE;
}
}
答案 3 :(得分:0)
以下功能对我来说就像一个魅力:
func validateEmailWithString(email: String) -> Bool {
let emailRegex: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailTest: NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailRegex)
return emailTest.evaluateWithObject(email)
}
答案 4 :(得分:0)
尝试
if (![self validateEmailWithString:txtEmail.text])
{
//@"Please Enter a Valid Email Address"];
}
- (BOOL)validateEmailWithString:(NSString*)checkString
{
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];
return [emailTest evaluateWithObject:checkString];
}