ios中的电子邮件和密码验证

时间:2016-02-09 07:29:36

标签: ios objective-c validation

因为我是ios的新手。我知道之前已经问过这个问题。但我在这些方面遇到了困难。我想验证电子邮件和密码,当用户输入正确的电子邮件和密码(8到10个字符有正则表达式)时,按钮将启用,可以转到下一页。但如果电子邮件和密码不正确,则按钮将被禁用。因为我已经尝试过这个但是当用户点击按钮时它会起作用。

-(IBAction)clickOn:(id)sender
{
    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:email.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];
    }
    HomePage *home=[[HomePage alloc]initWithNibName:@"HomePage" bundle:nil];
    [self.navigationController pushViewController:home animated:YES];
}

因为我已经像这样做了

IBOutlet UITextField *email;
IBOutlet UITextField *pwd;

但我不知道如何使用方法附加textfield以及如何在验证电子邮件和密码时禁用按钮,并在两者完美时启用它们。

2 个答案:

答案 0 :(得分:1)

在UIViewController中添加UITexTField的委托方法,并在viewDidLoad中将textfield委托设置为self,或者也可以在InterfaceBuilder中设置。默认情况下,应在viewDidLoad方法中将click按钮设置为disable。同时设置按钮的IBOutlet。

self.email.delegate = self;
[self.button setEnabled:NO];

在ViewController中添加此UITextfield的委托方法

 - (void)textFieldDidEndEditing:(UITextField *)textField{
        if([textField.text length]>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:email.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];
                [button setEnabled:NO]
            }else{
                [button setEnabled:YES]
            }
        }else{
             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"enter the Valid Mail id" message:@"Please Enter Valid Email Address." delegate:nil cancelButtonTitle:@"okay" otherButtonTitles:nil];
                [alert show];
        }
}

您还可以在上述方法中添加密码的逻辑。我引导您使用实现功能的技术。

然后仅为导航目的实现onClick方法

-(IBAction)clickOn:(id)sender
{
    HomePage *home=[[HomePage alloc]initWithNibName:@"HomePage" bundle:nil];
    [self.navigationController pushViewController:home animated:YES];
}

答案 1 :(得分:0)

首先,您需要在界面中设置UITextfield委托。

enter image description here

然后将代理人分配到 viewdidload 方法中的文本字段

email.delegate=self;
pwd.delegate=self;

然后你需要在UITextfield委托方法中为你进行正则表达式验证的条件,例如,

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    [textField addTarget:self
                  action:@selector(textFieldDidChange:)
        forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (![email condition not matched ]|| [[pwd.text length]>8]) {


        NSLog(@"Button Hide");
    } else {
           NSLog(@"Button Show");

    }
}