如何使用textfield()函数检查swift中我的textfield的所有验证
因此,我有一个问题,我想检查我的文本字段的验证。如果验证已经正确,按钮将被启用,如果验证不正确,按钮将被禁用。问题是,如果我已经通过了一个文本字段的验证,并希望移动到另一个文本字段。验证仅在我单击文本字段的位置检查验证。如果我没有更正验证并转到另一个文本字段,则应禁用按钮,如果我更正了另一个文本字段的验证,则按钮将启用。我希望检查所有验证我的所有文本字段 我的代码中的电子邮件是可选的 这是我的密码如何使用textfield()函数检查swift中我的textfield的所有验证,swift,Swift,因此,我有一个问题,我想检查我的文本字段的验证。如果验证已经正确,按钮将被启用,如果验证不正确,按钮将被禁用。问题是,如果我已经通过了一个文本字段的验证,并希望移动到另一个文本字段。验证仅在我单击文本字段的位置检查验证。如果我没有更正验证并转到另一个文本字段,则应禁用按钮,如果我更正了另一个文本字段的验证,则按钮将启用。我希望检查所有验证我的所有文本字段 我的代码中的电子邮件是可选的 这是我的密码 func textField(_ textField: UITextField, shouldCh
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let set = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// Casting String as NSString to be able to replace characters using NSRange easily.
guard let text = textField.text as NSString? else { return true }
// Get the length of the final text
let finalTextCount = text.replacingCharacters(in: range, with: string).count
// Setup the text count for the field depending on which one was edited
let nomorTextFieldCount = textField == nomorTextField ? finalTextCount :
nomorTextField.text!.count
let emailTextFieldCount = textField == emailTextFeild ? finalTextCount :
emailTextFeild.text!.count
if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
nomorTextField.tag=1
emailTextFeild.tag=2
if textField.tag==1{
if nomorTextFieldCount == 0 {
floatingLabelTextField.errorMessage = "Nomor can't empty"
nextButton.isEnabled = false
}else if nomorTextFieldCount < 10 {
floatingLabelTextField.errorMessage = "Nomor can't less than 10"
nextButton.isEnabled = false
}else if (nomorTextFieldCount > 12 ) {
floatingLabelTextField.errorMessage = "Nomor can't more than 12"
nextButton.isEnabled = false
}
func textField(textField:UITextField,shouldChangeCharacters范围:NSRange,replacementString:string)->Bool{
let set=CharacterSet(charactersIn:“abcdefghijklmnopqrstuvxyzabefghijklmnopqrstuvxyz”)
//将字符串强制转换为NSString,以便能够轻松使用NSRange替换字符。
guard let text=textField.text作为NSString?否则{return true}
//获取最终文本的长度
让finalTextCount=text.replacingCharacters(在:范围内,带:字符串)。计数
//根据编辑的字段设置字段的文本计数
让nomorTextFieldCount=textField==nomorTextField?finalTextCount:
nomorTextField.text!.count
让emailTextFieldCount=textField==emailTextFeild?finalTextCount:
emailTextFeild.text!.count
如果让floatingLabelTextField=textField作为?SkyFloatingLabelTextField{
nomorTextField.tag=1
emailtextfield.tag=2
如果textField.tag==1{
如果nomorTextFieldCount==0{
floatingLabelTextField.errorMessage=“Nomor不能为空”
nextButton.isEnabled=false
}如果nomorTextFieldCount<10,则为else{
floatingLabelTextField.errorMessage=“Nomor不能小于10”
nextButton.isEnabled=false
}否则,如果(nomorTextFieldCount>12){
floatingLabelTextField.errorMessage=“Nomor不能超过12”
nextButton.isEnabled=false
}
创建一个包含文本字段的类,覆盖一个方法验证,并对每个文本文件进行验证。注意,您创建了多个同名帐户以重复post问题。您可以使用一个变量val_Count,当验证成功时,将其增加一个…当您的val_Coount==TF的数量,然后启用按钮,否则禁用它。