[ad_1]
When I type email address in email textfield, it goto the default case every time that’s why I am not allowed to enter “@” symbol.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField.isValidInput(text: string, field: textField) {
return true
} else {
return false
}
}
extension UITextField {
func isValidInput(text: String, field: UITextField) -> Bool {
let englishAndDigitsRegEx: String
switch field.textContentType {
case UITextContentType.emailAddress:
englishAndDigitsRegEx = "^[a-zA-Z0-9@.]*$"
case UITextContentType.password:
return true
default:
englishAndDigitsRegEx = "^[a-zA-Z0-9]*$"
}
let englishAndDigitsTest = NSPredicate(format:"SELF MATCHES %@", englishAndDigitsRegEx)
return englishAndDigitsTest.evaluate(with: text)
}
}
I am expecting that make modular code that could be access from any view controller field to validate input according their textfield using regular expression.
[ad_2]