Swift - validating UITextField

后端 未结 5 1505
梦谈多话
梦谈多话 2020-11-30 11:58

I have these outlets in my app:

@IBOutlet var name1: UITextField!

@IBOutlet var name2: UITextField!

@IBOutlet var name3: UITextField!

@IBOutlet var name4:         


        
相关标签:
5条回答
  • 2020-11-30 12:19

    Try this code. Hope you will get what to do.

    func updateSaveButtonState() {
        let text = titleTextField.text ?? ""
        saveButton.isEnabled = !text.isEmpty
    }
    
    
    @IBAction func textEditingChanged(_ sender: UITextField) {
        updateSaveButtonState()
    }
    
    @IBAction func returnPressed(_ sender: UITextField) {
        titleTextField.resignFirstResponder()
    }
    
    0 讨论(0)
  • 2020-11-30 12:27

    Take a look at UITextFieldValidator. I have created a validator which covers almost each validation required in an app. Also its in swift. So go ahead and check it out.

    0 讨论(0)
  • 2020-11-30 12:38

    The textField:shouldChangeCharactersInRange: method gets called BEFORE the contents of the text field have changed.

    You should special-case the current text field and figure out if it's new contents are going to be blank after the edit.

    Something like this:

    textField.text.length > range.length - replacementString.length
    

    (That's off the top of my head, and not debugged. Plus you would have to come up with logic that makes sure all the other fields are non-blank, and use the logic above to test the contents of the current field.)

    0 讨论(0)
  • 2020-11-30 12:40

    Alternatively, you can use this, which is called every time a key is pressed:

    name1.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    name2.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    name3.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    name4.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
    
    
    func textFieldDidChange(textField: UITextField) {
        if name1.text?.isEmpty || name2.text?.isEmpty || name3.text?.isEmpty || name4.text?.isEmpty {
            //Disable button
        } else {
            //Enable button
        }
    }
    
    0 讨论(0)
  • 2020-11-30 12:41

    Swift 4 - Here is how I have solved it trying to avoid long conditionals - This will also allow you to do realtime validation on each individual textfield, unlike the accepted answer, you can update your UI according to what the user is typing.

    let textfields : [UITextField] = [name1, name2, name3, name4]
    for textfield in textfields {
      textfield.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
    }
    
    
    @objc func textFieldDidChange(_ textField: UITextField) {
      //set Button to false whenever they begin editing
      yourButton.isEnabled = false
      guard let first = textFields[0].text, first != "" else {
            print("textField 1 is empty")
            return
        }
        guard let second = textFields[1].text, second != "" else {
            print("textField 2 is empty")
            return
        }
        guard let third = textFields[2].text, third != "" else {
            print("textField 3 is empty")
            return
        }
        guard let forth = textFields[3].text, forth != "" else {
            print("textField 4 is empty")
            return
        }
        // set button to true whenever all textfield criteria is met.
        yourButton.isEnabled = true
    
    }
    
    0 讨论(0)
提交回复
热议问题