I am starting to program on iOS. I create a registration form and perform a client-side check in the text box. I want to show an error message under the text box in the shortcut. I searched alot and got this question , but it’s hard for me to understand, and I think there should be an easier way to achieve this. I can show the message in the label, but the problem is that it does not hide when the focus is again set to this text box.
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if(textField == _emailTextField)
{
if(![self validateEmailWithString:_emailTextField.text])
{
[_errorMessageLabel setText:@"please enter valid Email"];
}
}
else
{
}
}
- (bool)validateEmailWithString:(NSString *)emailStr
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}
source
share