我有一个类型为OctoberPad的文本字段。但是,如果用户键入“。”,“ ..”,...,则我的数据库将收到错误。
如果uesr输入了以上值,我想提醒“无效输入”语法。
当前,我的func textFieldShouldEndEditing代码。如何添加代码?
default_delete<T>.
答案 0 :(得分:1)
我从here那里获得了参考
首先将UITextFieldDelegate
添加到您的控制器中
class ViewController: UIViewController, UITextFieldDelegate
将此添加到viewDidLoad
yourTextField.delegate = self
然后
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let isNumber = CharacterSet.decimalDigits.isSuperset(of: CharacterSet(charactersIn: string))
let withDecimal = (
string == NumberFormatter().decimalSeparator &&
textField.text?.contains(string) == false
)
return isNumber || withDecimal
}
答案 1 :(得分:0)
步骤:1
class ViewController: UIViewController, UITextFieldDelegate
步骤:2 (ViewDidLoad)
MyTextField.delegate = self
步骤:3
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.characters.count > 0 {
var allowedCharacters = CharacterSet.alphanumerics
let unwantedStr = string.trimmingCharacters(in: allowedCharacters)
return unwantedStr.characters.count == 0
}
return true
}
这也可以将字符串粘贴到您的文本字段中