在 swift 中对文本字段实施两个限制
Implement two restriction on textfield in swift
我有一个文本字段,我想在其中限制最大长度和特殊字符。我已经实现了最大长度,但效果很好,但是当我实现特殊字符限制时,它没有捕捉到我的代码的条件,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstNameTxt
{
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 15
}
else if textField == firstNameTxt
{
let allowedCharacters = CharacterSet(charactersIn:"+0123456789 !@#$%^&*()_+{}[]|\"<>,.~`/:;?-=\¥'£•¢")//Here change this characters based on your requirement
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
return true
}
如何同时设置两个限制?
我建议使用 regex
来阻止在 UITextField
中输入不需要的字符。 Regex Pattern 是用最大条件验证内容而不是编写逻辑代码的最佳方法。
UITextFieldDelegate:
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange,
with: string)
return validateString(updatedText)
}
return true
}
验证:
func validateString(_ string: String?) -> Bool {
let regex = "^[a-zA-Z]{0,15}$"
let predicate = NSPredicate(format:"SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: string)
return isValid
}
编辑
在正则表达式中,我们可以在 {}
大括号中提及字符范围。在给定的场景中,我们提到 {0,15}
作为范围。
要了解有关正则表达式模式的更多信息,请查看 https://www.geeksforgeeks.org/write-regular-expressions/
我有一个文本字段,我想在其中限制最大长度和特殊字符。我已经实现了最大长度,但效果很好,但是当我实现特殊字符限制时,它没有捕捉到我的代码的条件,
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if textField == firstNameTxt
{
guard let textFieldText = textField.text,
let rangeOfTextToReplace = Range(range, in: textFieldText) else {
return false
}
let substringToReplace = textFieldText[rangeOfTextToReplace]
let count = textFieldText.count - substringToReplace.count + string.count
return count <= 15
}
else if textField == firstNameTxt
{
let allowedCharacters = CharacterSet(charactersIn:"+0123456789 !@#$%^&*()_+{}[]|\"<>,.~`/:;?-=\¥'£•¢")//Here change this characters based on your requirement
let characterSet = CharacterSet(charactersIn: string)
return allowedCharacters.isSuperset(of: characterSet)
}
return true
}
如何同时设置两个限制?
我建议使用 regex
来阻止在 UITextField
中输入不需要的字符。 Regex Pattern 是用最大条件验证内容而不是编写逻辑代码的最佳方法。
UITextFieldDelegate:
func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String) -> Bool {
if let text = textField.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange,
with: string)
return validateString(updatedText)
}
return true
}
验证:
func validateString(_ string: String?) -> Bool {
let regex = "^[a-zA-Z]{0,15}$"
let predicate = NSPredicate(format:"SELF MATCHES %@", regex)
let isValid = predicate.evaluate(with: string)
return isValid
}
编辑
在正则表达式中,我们可以在 {}
大括号中提及字符范围。在给定的场景中,我们提到 {0,15}
作为范围。
要了解有关正则表达式模式的更多信息,请查看 https://www.geeksforgeeks.org/write-regular-expressions/