How to show textbox validation error message in shortcut

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])
        {
            //showing error on a label
            [_errorMessageLabel setText:@"please enter valid Email"];
        }
    }
    else
    {
        //valid email
    }
}

- (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];
}
+4
source share
2 answers

Yes, there is a simple solution. Just hide your shortcut if the focus parameter is set UITextFieldbelow the delegate method

-(void)textFieldDidBeginEditing:(UITextField *)textField {
    _errorMessageLabel.hidden = YES;

    ...

}
+3

, , ( ) ?

, "textFieldDidBeginEditing",

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    [_errorMessageLabel setText:@""];
}

,

- (void) textFieldDidBeginEditing:(UITextField *)textField {
    _errorMessageLabel.hidden = YES;
}
+2

All Articles