Deepak Thakur
Deepak Thakur

Reputation: 3701

iPhone : How to validate email id when the user goes to the next textField and check if the email id is valid there only before he proceeds further

I have lots of textfields in one view and instead of user filling all the details and hit save button, I want to validate the email id entered there only. How to give the error message in the label when the user goes to the next textField

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:self.emailidTexField.text] == NO)
{
   // UILabel besides the text field and when the user goes to next field logic here.
}

Upvotes: 1

Views: 1128

Answers (7)

Vicky
Vicky

Reputation: 170

This may help... try this

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:emailidTexField])
     {
        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:self.emailidTexField.text] == NO)
             {
               [emailidTexField becomeFirstResponder];
             }
      else{
               [Yournexttextfield becomeFirstResponder];
           } 
      }
}

Upvotes: 2

Paras Joshi
Paras Joshi

Reputation: 20551

use this bellow code..

Here when you click on return or Next key of keyBoard at that time this bellow textFieldShouldReturn: UITextField delegate method called and you can write any validation or code with its condition bellow ...

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField==self.emailidTexField) {
        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:self.emailidTexField.text] == NO)
        {
           // UILabel besides the text field and when the user goes to next field logic here.
           [self.emailidTexField resignFirstResponder];
           [YourNextTextFieldName becomeFirstResponder];
        }
        else{
           // Display message here that EmaiId is not valid
           return NO;
        }

    }
    return YES;
}

here i put the condition that if textField is self.emailidTexField then go inside and check validation if its true then it resign that textField keyboard and go cursor on NextTextField.

Upvotes: 2

Rahul Patel
Rahul Patel

Reputation: 5886

You have to set delegate of textfield and write following code in UITextField delegate method

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:emailidTexField])
     {
        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:self.emailidTexField.text] == NO)
             {
               [emailidTexField becomeFirstResponder];
             }
      else{
               [Yournexttextfield becomeFirstResponder];
           } 
      }
}

Upvotes: 0

IronManGill
IronManGill

Reputation: 7226

Simple when the user goes to the next UITextField . Check in the

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

whether it is the same UITextField or not , either you can set tags for the UITextField's to find out. Then just add a UIAlertView there like :-

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
          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:self.emailidTexField.text] == NO)
          {
                UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email is Invalid !!!  " delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
          }
    }

Upvotes: 1

Abhishek Goyal
Abhishek Goyal

Reputation: 96

 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:self.emailidTexField.text] == NO)
 {
    [your next field becomesfirstresponder];
 }

Make your next textfield firstresponder to move the control to next field.

Upvotes: 1

Wain
Wain

Reputation: 119031

You can add yourself as the delegate of the text field and perform your validation in textFieldDidEndEditing:. Be sure to call endEditing: on the view when the save button is pressed to ensure that the delegate method runs.

Upvotes: 1

Horst
Horst

Reputation: 1739

 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
      NSLog(@"%d",textFiled.tag);
      if(textField.tag==xxx){
    //check valid here
      }
      return YES;
  }

Upvotes: 1

Related Questions