简体   繁体   中英

How to validate textfield

在我的应用程序中,我将一个文本字段用于电子邮件,另一个用于用户名和按钮,在按钮事件下编写代码后输入数据单击按钮显示另一个视图,我的问题是编写电子邮件验证代码(这种格式为aaa@gmail.com)这些单击按钮时显示警报视图(msg-输入正确的电子邮件格式)但是我希望显示这些警报移动到下一个textfiled假设我以错误的格式输入电子邮件,并将电影输入到下一个字段,当时将显示用户名textfiled电子邮件警报视图。

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

Check Here. Email Validation

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM