在我的应用程序中,我将一个文本字段用于电子邮件,另一个用于用户名和按钮,在按钮事件下编写代码后输入数据单击按钮显示另一个视图,我的问题是编写电子邮件验证代码(aaa@gmail.com在此格式)这些警报视图是在按钮点击时显示的(msg-输入正确的电子邮件格式)但是我希望显示这些警报移动到下一个textfiled假设我以错误的格式输入电子邮件并将电影输入到下一个字段它当时用户名textfiled电子邮件警报视图将显示。
答案 0 :(得分:2)
使用NSPredicate
和Regex
:
- (BOOL)validateEmailString:(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];
}
对于以逗号(,)分隔的电子邮件:
- (NSMutableArray*)validateEmailWithString:(NSString*)emails
{
NSMutableArray *emails = [[NSMutableArray alloc] init];
NSArray *emailOfArray = [emails componentsSeparatedByString:@","];
for (NSString *email in emailOfArray)
{
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:email])
[emails addObject:email];
}
return [emails autorelease];
}
答案 1 :(得分:0)
点击此处。Email Validation
答案 2 :(得分:0)
我已经在editingDidEnd事件上验证了我的字段,并使用了以下代码:
- (IBAction) emailValidation:(id)sender {
NSString *eml=((UITextField *)sender).text;
NSString *regex = @"\\b([a-zA-Z0-9%_.+\\-]+)@([a-zA-Z0-9.\\-]+?\\.[a-zA-Z]{2,6})\\b";
NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL x= [regextest evaluateWithObject:eml];
if (x==FALSE) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Errror!" message:@"You have entered incorrect email ID" delegate:self cancelButtonTitle:@"Wanna Correct" otherButtonTitles:nil];
[alert show];
[emailField becomeFirstResponder];
[alert release];
}
}
-(IBAction)passwordValidator:(id)sender{
NSString *pwd=[NSString stringWithString:passwordField.text];
int lngth=[pwd length];
int minlength=6;
NSString *regex = @"\\b([a-zA-Z0-9]+)\\b";
NSPredicate * regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL x= [regextest evaluateWithObject:pwd];
if (lngth>=minlength) {
NSLog(@"passoword length is enough");
if (x==FALSE) {
NSLog(@"Special charector check enabled");
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No Special Charectors" message:@"please don't use special charectors" delegate:self cancelButtonTitle:@"Wanna Correct" otherButtonTitles:nil];
[alert show];
[alert release];
[passwordField becomeFirstResponder];
[self.view addSubview:passwordField];
}
}
else {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Poor length" message:@"Password length must not be less than 8.." delegate:self cancelButtonTitle:@"Wanna Correct" otherButtonTitles:nil];
[alert show];
[alert release];
[passwordField becomeFirstResponder];
}
}
尝试这些,然后打电话给任何相关的事件,你会得到想要的结果。好运:)