验证UITextField中的电子邮件地址

Ket*_*nde 11 iphone objective-c email-validation ios swift

我想在包含电子邮件地址的文本字段中进行验证.
在字符串"emailRegEx"中添加什么来限制用户在电子邮件地址中插入三个点或者我必须在方法中写入相同的内容.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string. 
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

- (BOOL)EmailValidationL:(NSString *)email
{
    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 *regExPredicate =
    [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
    a= [regExPredicate evaluateWithObject:email];
    return a;
}
Run Code Online (Sandbox Code Playgroud)

sch*_*sch 18

有更好的方法来验证电子邮件地址.

- (BOOL)validateEmailWithString:(NSString*)email
{
    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:email];
}
Run Code Online (Sandbox Code Playgroud)


Anb*_*hik 8

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

if ([emailTest evaluateWithObject:yourtextFieldName.text] == NO) 
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"enter the Valid Mail id" message:@"Please Enter Valid Email Address." delegate:nil cancelButtonTitle:@"okay" otherButtonTitles:nil];
[alert show];

}
Run Code Online (Sandbox Code Playgroud)

迅速

 var emailReg: String = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
var emailTest: NSPredicate = NSPredicate(format: "SELF MATCHES %@", emailReg)

if emailTest.evaluateWithObject(yourtextFieldName.text!) == false {
var alert: UIAlertView = UIAlertView(title: "enter the Valid Mail id", message: "Please Enter Valid Email Address.", delegate: nil, cancelButtonTitle: "okay", otherButtonTitles: "")
alert.show()
}
Run Code Online (Sandbox Code Playgroud)

Swift3

let emailReg = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
let emailTest = NSPredicate(format: "SELF MATCHES %@", emailReg)
if emailTest.evaluate(withObject: yourtextFieldName.text) == false {
 UIAlertView(title: "enter the Valid Mail id", message: "Please Enter Valid Email Address.", delegate: nil, cancelButtonTitle: "okay", otherButtonTitles: "").show()
 }
Run Code Online (Sandbox Code Playgroud)


Gee*_*eek 5

此正则表达式将限制用户在电子邮件地址(或任意数量的连续点)中插入三个点,例如

username@example...com
Run Code Online (Sandbox Code Playgroud)

带点的另一个规则是用户名和域名不能以点开头或结尾,例如

.username.@.example.com
Run Code Online (Sandbox Code Playgroud)

互联网上的许多正则表达式允许无效的电子邮件地址.在采用之前,您可能希望尝试使用一些无效的电子邮件地址进行测试,以查看它们是否被拒绝.你可以使用我在这里提到的两个例子作为开始.

- (BOOL)isValidEmail:(NSString *)email
{
    NSString *regex1 = @"\\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,4}\\z";
    NSString *regex2 = @"^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*";
    NSPredicate *test1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex1];
    NSPredicate *test2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex2];
    return [test1 evaluateWithObject:email] && [test2 evaluateWithObject:email];
}
Run Code Online (Sandbox Code Playgroud)

请参阅使用Objective-C中的正则表达式验证电子邮件地址.


mem*_*ons 5

iOS 已经为许多不同的数据类型内置了数据检测器。尽管电子邮件不是这些类型之一,但电子邮件地址属于链接的子类型。知道这一点,检查电子邮件地址很容易:

NSError *error = nil;
NSDataDetector *detector = 
   [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];

NSString *string = @"johndoe@gmail.com";
[detector enumerateMatchesInString:string
                           options:kNilOptions
                             range:NSMakeRange(0, [string length])
                        usingBlock:^(NSTextCheckingResult *result, 
                                     NSMatchingFlags flags, BOOL *stop) 
{
  if([result.URL.absoluteString rangeOfString:@"mailto:"].location != NSNotFound)
  {
    NSLog(@"Match: %@", result);
  }
}];
Run Code Online (Sandbox Code Playgroud)

Matt Thompson 的 NSHipster 博客有一篇关于NSDataDetector其用法的优秀文章


小智 3

你可以通过使用 String 的扩展来做到这一点

extension String {
  var isEmail: Bool {
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailTest.evaluate(with: self)
  }
}
Run Code Online (Sandbox Code Playgroud)

现在你可以用它作为

if emailtext.isEmail {  
  print("email is valid")    
} else {
  print("email is not valid") 
}
Run Code Online (Sandbox Code Playgroud)