我将 SkyFloatingLabeltextField 用于文本字段。我想检查所有文本字段是否有效,然后我想调用下一个 viewController。所以请告诉我如何检查
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {}
是真的还是假的???下面是我的代码
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if let text = username.text {
let message = Comman.matches(for: "[a-zA-Z]+[0-9a-zA-Z._%+-]+[a-zA-Z0-9]+@[a-zA-Z0-9-]+\\.[a-zA-Z]{1,6}", in: text)
print(message.count);
print(message)
if let floatingLabelTextField = username as? SkyFloatingLabelTextField {
if(message.count != 1) {
floatingLabelTextField.errorMessage = "Please enter valid email id"
}
else {
// The error message will only disappear when we reset it to nil or empty string
floatingLabelTextField.errorMessage = ""
}
}
}
if let text = password.text {
let message = Comman.matches(for: "^(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&])(?=.*\\d).{8,16}$", in: text)
if let floatingLabelTextField = password as? SkyFloatingLabelTextField {
if(message.count != 1) {
floatingLabelTextField.errorMessage = "Please enter valid Password"
}
else {
// The error message will only disappear when we reset it to nil or empty string
floatingLabelTextField.errorMessage = ""
}
}
}
if let text = firstName.text {
let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789")
if let floatingLabelTextField = firstName as? SkyFloatingLabelTextField {
if text.rangeOfCharacter(from: characterset.inverted) != nil {
floatingLabelTextField.errorMessage = "Please enter valid Name"
}
else {
// The error message will only disappear when we reset it to nil or empty string
floatingLabelTextField.errorMessage = ""
}
}
}
if let text = lastName.text {
let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789")
if let floatingLabelTextField = lastName as? SkyFloatingLabelTextField {
if text.rangeOfCharacter(from: characterset.inverted) != nil {
floatingLabelTextField.errorMessage = "Please enter valid Name"
}
else {
// The error message will only disappear when we reset it to nil or empty string
floatingLabelTextField.errorMessage = ""
}
}
}
if let text = mobileNumber.text {
let characterset = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789")
if let floatingLabelTextField = mobileNumber as? SkyFloatingLabelTextField {
if text.rangeOfCharacter(from: characterset.inverted) != nil {
floatingLabelTextField.errorMessage = "Please enter valid Mobile number"
}
else {
// The error message will only disappear when we reset it to nil or empty string
floatingLabelTextField.errorMessage = ""
}
}
}
return true
}
我的按钮就像
@IBAction func signUpButtonPressed(_ sender: Any) { if username.text == "" {
if let floatingLabelTextField = username as? SkyFloatingLabelTextField {
floatingLabelTextField.errorMessage = "Please enter UserName"
}
}
if password.text == "" {
if let floatingLabelTextField = password as? SkyFloatingLabelTextField {
floatingLabelTextField.errorMessage = "Please enter Password"
}
}
if firstName.text == "" {
if let floatingLabelTextField = firstName as? SkyFloatingLabelTextField {
floatingLabelTextField.errorMessage = "Please enter First Name"
}
}
if lastName.text == "" {
if let floatingLabelTextField = lastName as? SkyFloatingLabelTextField {
floatingLabelTextField.errorMessage = "Please enter Last Name"
}
}
if mobileNumber.text == "" {
if let floatingLabelTextField = mobileNumber as? SkyFloatingLabelTextField {
floatingLabelTextField.errorMessage = "Please enter Mobile Number"
}
}
}
那么有效时如何处理????