我试图在我的textFields上执行文本验证以供用户登录和注册,并且我希望根据用户输入的内容显示错误文本。我一直试图找出在文本字段中或周围显示错误文本的默认实现是否是我可以构建的,但我没有找到任何东西。是否有不依赖外部库的解决方案?
我已经有了验证textFields的逻辑,但是我只需要向用户显示消息。这就是我所拥有的:
@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.")
}
}发布于 2022-06-22 17:26:00
您可以使用UIAlertController显示错误消息。
用于textField
yourTextfield.text = "Error Message"https://stackoverflow.com/questions/72719577
复制相似问题