我正在尝试将验证放在包含电子邮件地址的文本字段中。
在字符串“emailRegEx”中添加什么来限制用户在电子邮件地址中插入三个点或者我必须在方法中写入相同。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string.
这是我的代码:
- (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;
}
答案 0 :(得分:18)
验证电子邮件地址的效果更好ways。
- (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];
}
答案 1 :(得分: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];
}
<强>夫特强>
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()
}
<强> 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()
}
答案 2 :(得分:5)
此正则表达式将限制用户在电子邮件地址(或任意数量的连续点)中插入三个点,例如
username@example...com
带dot的另一个规则是用户名和域名不得以点开头或结尾,例如
.username.@.example.com
互联网上的许多正则表达式都允许无效的电子邮件地址。在采用之前,您可能希望尝试使用一些无效的电子邮件地址进行测试,以查看它们是否被拒绝。你可以使用我在这里提到的两个例子作为开始。
- (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];
}
请参阅validate email address using regular expression in Objective-C。
答案 3 :(得分:3)
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);
}
}];
Matt Thompson的NSHipster博客在NSDataDetector
上有excellent article及其用途。
答案 4 :(得分:1)
您可以使用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)
}
}
现在您可以将其用作
emailtext.isEmail {
print("email is valid")
} else {
print("email is not valid")
}
答案 5 :(得分:0)
在memmons'answer上构建,以下代码应验证整个电子邮件字符串,假设我们可以信任NSDataDetector
- (BOOL)isPlausibleEmailAddress
{
NSError *error = nil;
NSDataDetector *detector =
[NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:&error];
NSRange matchRange = [detector rangeOfFirstMatchInString:self options:kNilOptions range:NSMakeRange(0, [self length])];
return matchRange.length == [self length];
}