9

I am creating login screen using Objective C in which i want to implement validation for User name(E-Mail) and password.How to implement this in very simple way.

Mohith P
  • 585
  • 5
  • 14

2 Answers2

20

You can always make use textField shouldChangeCharactersInRange delegate to handle the number of characters allowed in textField. Have look at the solution provided below :) Hope it helps

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        if(textField == self.emailTextField){
            if (textField.text.length < 30 || string.length == 0){ 
                return YES;
            }
            else{
                return NO;
            }
        }
    }

EDIT

As per your comments you are not recieveing the textField delegates, so here is what you can do :)

In your ViewController, confirm the UITextFieldDelegate using,

YourViewController : UIViewController <UITextFieldDelegate>

In your viewDidLoad or ViewWillAppear set the textField delegate as self.

self.emailTextField.delegate = self;
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
0

If you click the Validate Button, set the validate button Action and see the below code,first check the two textfield empty or not, if you give text then again check its valid email type or not, then all conditions are true, then put your code and run,

-(IBAction)action:(id)sender
{
    if (tfMail.text.length == 0 || tfPass.text.length == 0) 
    {
         [self validatetextfield];
    }
    else if (![tfMail.text isEqualToString:@""])
    {

         NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
         NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
                //Valid email address
         if ([emailTest evaluateWithObject:tfMail.text] == YES)
         {
              //Its validated put your success code,
         }
         else
         {
              UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Test!" message:@"Please Enter Valid Email Address. \nex. fdsjfkd@mail.com" preferredStyle:UIAlertControllerStyleAlert];
              UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self dismissViewControllerAnimated:YES completion:nil];
              }];

              [aler addAction:action];

              [self presentViewController:aler animated:YES completion:nil];
                    //not valid email address
          }
    }
}

Alert Message Method:

-(void) validatetextfield
{
    if (tfMail.text.length==0) {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Email Field Empty!" message:@"Please Enter the Email" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];

        [aler addAction:action];

        [self presentViewController:aler animated:YES completion:nil];
        [tfMail becomeFirstResponder];
    }
    else if (tfPass.text.length==0)
    {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"Password Field Empty!" message:@"Please Enter the Password" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           [self dismissViewControllerAnimated:YES completion:nil];
        }];

        [aler addAction:action];

        [self presentViewController:aler animated:YES completion:nil];
        [tfPass becomeFirstResponder];
    }
}

its successfully working for me, hope its helpful.

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30