Narendra Pandey
Narendra Pandey

Reputation: 377

Email and password validation in ios

As i am new to ios.I know this question has been asked before. but i am having difficulty among those. i want to validate email and password, when user enter correct email and password(8 to 10 character having regular expression) then button will enable and can go to next page. but if email and password is not correct then button will be disabled. as i have tried this but it will work when user click on button.

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

as i have taken textfied as like this

IBOutlet UITextField *email;
IBOutlet UITextField *pwd;

But i don't know how to attach textfield with method and how to disable button while validating email and password and enable them when both are perfect.

Upvotes: 0

Views: 2110

Answers (2)

Badal Shah
Badal Shah

Reputation: 7612

For first , you need to set UITextfield delegate in interface like this.

enter image description here

Then assign delegate to your Textfield in viewdidload method.

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

and then you need to put you condition for regex verification in UITextfield delegate method like,

-(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");

    }
}

Upvotes: 0

Shehzad Ali
Shehzad Ali

Reputation: 1845

Add delegate method of UITexTField in your UIViewController and set textfield delegates as self in your viewDidLoad or you can also set in InterfaceBuilder. By default on click button should be set disable in your viewDidLoad method. Also set the IBOutlet of your button.

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

add this delegate method of UITextfield in your ViewController

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

You can also add the logic of your password in above mentioned method. I have guided you with the technique of achieving your functionality.

Then Implement the onClick method only for navigation purpose

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

Upvotes: 1

Related Questions