I am trying to perform text validation on my textFields for user login and sign up and I want to display error text depending on what the user inputs. I have been trying to find if there is any sort of default implementation for displaying error text in or around a textfield that I can build off of but I have not been able to find anything. Is there a solution that doesn't involve relying upon external libraries?
I already have the logic for validating the textFields but I just need to display messages to the user. This is what I have:
@IBAction func signUpButton(_ sender: UIButton) {
if (emailTextField.text == "") {
print("Please enter an email.")
} else if (passwordTextField.text == "") {
print("Please enter a password.")
} else if (confirmPasswordTextField.text == "") {
print("Please confirm your password.")
} else if (confirmPasswordTextField.text != passwordTextField.text) {
print("Please ensure that your passwords are matching.")
} else if (!emailTextField.isEmail()) {
print("Please enter a valid email address.")
} else if (!passwordTextField.isValidPassword()) {
print("Passwords must be at least 8 characters in length and must contain at least one letter and one number.")
}
}
CodePudding user response:
your can use UIAlertController
to show the error messages.
For textField use
yourTextfield.text = "Error Message"
CodePudding user response:
If you're looking for a built-in solution that is really low-effort like
inputView.validationErrorMessage = "you must enter an email"
then no, there is no such thing built in to UIKit. However there are many built-in mechanisms that can be very easily used to show error messages.
For example, UIAlertController can show a modal popup with possible options for them to choose from. Another approach is to add labels or imageviews that indicate a validation error and use the isHidden property to show/hide them at the appropriate time.