Following is my code:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let prospectiveText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
if prospectiveText.validateAsPerRegExpression(regExpression: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]{}#%^*+=_\\|~<>€£¥•-/:;()$&@\".,?!\'") == false {
return false
}
return true
}
extension String {
func validateAsPerRegExpression(regExpression: String) -> Bool {
let floatExPredicate = NSPredicate(format:"SELF MATCHES %@", regExpression)
return floatExPredicate.evaluate(with: self)
}
}
Getting the following crash:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_INVALID_RANGE (string e, pattern ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]{}#%^*+=_\|~<>€£¥•-/:;()$&@".,?!', case 0, canon 0)'
I think i'm forming regular expression in wrong way, Any idea on the fix?
Actually my intension is to avoid user to enter any other language input from keyboard, but all the keys in normal keyboard. If there is any better way to do it, please let me know. Thanks.