如何验证文本字段

发布于 2024-10-31 03:40:37 字数 316 浏览 1 评论 0原文

在我的应用程序中,我使用一个文本字段作为电子邮件,另一个文本字段作为用户名和按钮,输入数据后在按钮事件下编写代码单击按钮显示另一个视图,我的问题是编写电子邮件验证代码([电子邮件受保护] 以这种格式)这些警报视图在单击按钮时显示(消息输入正确的电子邮件格式),但我想要显示这些警报移动到下一个文本字段假设我以错误的格式输入了电子邮件并将电影发送到下一个字段,此时将显示电子邮件警报视图的用户名文本字段。

In my App I take one textfield for email and another one for username and button, write code under button event after enter data click button display another view,my problem is write code for email validation([email protected] in this format) these alert view is display when button click (msg-enter correct email format) but I want display these alert move to next textfiled suppose I entered email in wrong format and movie to next field it username textfiled at that time email alert view will display.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是一片海 2024-11-07 03:40:37

使用 NSPredicateRegex

- (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];
}

Use NSPredicate and 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];
}

For emails separated by a comma (,):

- (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];
}
我一直都在从未离去 2024-11-07 03:40:37

我已经在 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];
}
}

尝试这些,然后在任何相关事件上调用,您将获得所需的结果。祝你好运 :)

I have validated my fields on editingDidEnd event and used following code:

 - (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];
}
}

Try these, call then on any relevant event, You'll have desired result. good Luck :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文