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;"**