0

I'm writing a signup view controller for my app. I needed to validate the form. I got the idea that setting a selector method for text value change should work for different textfields containing the form data. I saw old questions and stuff on google and this is what I have so far

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.passwordInput.secureTextEntry = YES;
    self.btnDone.enabled = NO; //Done button needs to be disabled until form is properly validated
    self.emailInput.delegate = self; //emailinput is the property attached to Email textfield of the form
    self.passwordInput.delegate = self;
    emailCheck = NO;
    passwordCheck = NO;
    [self.emailInput addTarget:self action:@selector(formValidation) forControlEvents:UIControlEventValueChanged];
    [self.passwordInput addTarget:self action:@selector(formValidation) forControlEvents:UIControlEventValueChanged];
    // Do any additional setup after loading the view from its nib.
}

-(void) formValidation {

    NSString *regex = @"[^@]+@[A-Za-z0-9.-]+\\.[A-Za-z]+";
    NSPredicate *emailPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    if(self.passwordInput.text.length >7)
    {
        passwordCheck = YES;
    }
    if([emailPredicate evaluateWithObject:self.emailInput.text])
    {
        emailCheck = YES;
    }
    if (self.passwordInput.text.length<7 || ![emailPredicate evaluateWithObject:self.emailInput])
    {
    self.warningLabel.text = @"Please enter a valid email/at least 8 character password";
    }
    if(passwordCheck == YES && emailCheck ==YES)
    {
        self.btnDone.enabled = YES;//button is enabled
    } }

Now the problem is that the event is not firing off. Nothing happens when enter the data. Can anyone guide me what I'm doing wrong here?
P.s. i don't quite understand UITextFieldTextDidChangeNotification. If someone can suggest an alternative solution or explain that concept for me, it'd be awesome I just tried forControlEvents:UIControlEventEditingChanged, the app crashes with error that it can't perform regular expression.**"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object <UITextField: 0x9ae8610;"**

Noor
  • 2,071
  • 19
  • 28
NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • Look at the second non-accepted answer. http://stackoverflow.com/questions/7010547/uitextfield-text-change-event – ZeMoon Oct 15 '14 at 12:28
  • Did you include UITextfieldDelegate in header file? – Fawad Masud Oct 15 '14 at 12:28
  • Delegate is not required if you are using NSNotifications – ZeMoon Oct 15 '14 at 12:29
  • 1
    use UIControlEventEditingChanged instead UIControlEventValueChanged – Huy Nghia Oct 15 '14 at 12:29
  • I just used it. Please see above for the error I got. – NSNoob Oct 15 '14 at 12:31
  • The formValidation method will trigger an alert each time a character is entered. This will cause problems. Instead you should use the method when the editing ends. – ZeMoon Oct 15 '14 at 12:32
  • Sorry for being completely dumb ZeMoon, but i think you are telling me to connect the selector method to "Sent Event" editingDidEnd in textfield menu? I got that right? Somehow i can't drag the connector chord from that Sent Even menu to the selector. – NSNoob Oct 15 '14 at 12:36

3 Answers3

1

Set the delegate of textfields and use the following method

-(void)textFieldDidEndEditing:(UITextField *)textField
{
  [self validateForm:textField.text];
}

and change form validator function to

-(void) validateForm:(NSString *)inputString {

//validate inputString

}
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
  • It is the same error with UIControlEventEditingChanged. 2014-10-15 17:47:24.435 Mazeltov[2699:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object – NSNoob Oct 15 '14 at 12:45
  • change the line [emailPredicate evaluateWithObject:self.emailInput] to [emailPredicate evaluateWithObject:self.emailInput.text] and then tell me. – Fawad Masud Oct 15 '14 at 12:49
  • It worked. Thank you. That was so embarrassing. Sorry for wasting your time. – NSNoob Oct 15 '14 at 12:52
1

on button submit click of form even you can check validation like below.

 - (IBAction)pushToSignConfirmationScreen:(id)sender;
 {
     NSString *emailString = txt_Email.text;// storing the entered email in a string.
   / / Regular expression to checl the email format.
     NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailReg];

     if ([txt_ContactName.text length] == 0 || [txt_Address.text length] == 0
       || [txt_DBAName.text length] == 0 || [txt_City.text length] == 0
       || [txt_Email.text length] == 0 || [txt_Phone.text length] == 0
      || [txt_State.text length] == 0)
      {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"All fields are required to begin setting up a merchant account." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
       [alert show];

      return;
   }

  else if(([emailTest evaluateWithObject:emailString]!=YES)||[emailString isEqualToString:@""])
  {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Warning" message:@"Enter your email in abc@example.com format." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    txt_Email.text = @"";
    [btn_next setImage:[UIImage imageNamed:@"tag_icon_bt_up1.png"] forState:UIControlStateNormal];
    return;
}
else if([txt_ZipCode.text length]!=5)
{
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please enter a valid zip code." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

      txt_ZipCode.text = @"";
     return;
  }
  else if([txt_Phone.text length]!=10 )
  {
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please enter a       valid mobile number." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];

      txt_Phone.text = @"";
      return;
}
poojathorat
  • 1,200
  • 2
  • 9
  • 19
  • Thanks for the response but yeah it is simpler to validate the data on submission. My app requires the submission button to disabled until the Data passes the validation tests. I have applied the regex, as you can see above. – NSNoob Oct 15 '14 at 12:48
  • In textfieldDidEndEditing you can check validation for email & password text field & then if its true then enable submit button. – poojathorat Oct 16 '14 at 04:40
0

First set the delegate for textfield, then do check in Method:-

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

If textField is same then call your formValidation method.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
abhinav
  • 312
  • 1
  • 11