Swift - Validating Uitextfield

Swift - validating UITextField

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
}
}

How to validate UITextfield that satisfies 5 different conditions using shouldChangeCharactersInrange method?

I have created a regex based on the answers provided and it works. Could you guys please validate this.

  • Minimum 8 characters
  • Maximum 50 characters
  • Minimum 1 alphabet
  • Minimum 1 number
  • No special characters

Common Class

class Common: NSObject {

// MARK: - Password TextField Validations
static func isValidPassword(_ input: String) -> Bool {
print("validate Password: \(input)")
let passwordRegex = "^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,50}$"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: input)
}
}

ViewController Code

// MARK: - TextField delegate methods
extension SetNewPasswordViewController: UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
if textField == newPasswordTextfield {

if let text = newPasswordTextfield.text,
let textRange = Range(range, in: text) {
let updatedText = text.replacingCharacters(in: textRange,
with: string)
if Common.isValidPassword(updatedText) == true {
minimumCharacterValidationImage.isHighlighted = true
minimumAlphabetValidationImage.isHighlighted = true
minimumNumberValidationImage.isHighlighted = true
noSpecialCharacterValidationImage.isHighlighted = true
minimumCharacterValidationImage.isHighlighted = true
confirmButton.backgroundColor = UIColor(red:0, green:0.5, blue:0.54, alpha:1)
isValidPasswordStatus = true
} else {
minimumCharacterValidationImage.isHighlighted = false
minimumAlphabetValidationImage.isHighlighted = false
minimumNumberValidationImage.isHighlighted = false
noSpecialCharacterValidationImage.isHighlighted = false
confirmButton.backgroundColor = UIColor(red:0.4, green:0.47, blue:0.5, alpha:1)
isValidPasswordStatus = false
}
}

}
return true
}

}

EDIT : As there was no other answers and the one above works for the specified conditions, I'm marking my own answer as the accepted one. Just to be clear, I found the solution using the comments of Wiktor Stribiżew

what is better approach to UITextField validations on iOS?

Use validations for UITextFieldDelegate method like given below:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
return true
}

Or use custom validation function Here

Swift3: best way to validate the text entered by the user in a UITextField

You can set unique tag to your every text field and can compare in textFieldDidEndEditing or you can take IBOutlet of every textField and can compare it in textFieldDidEndEditing like,

 func textFieldDidEndEditing(textField: UITextField) {

// By tag
if textField.tag == 100 {

}

// OR

//by outlet
if textField == self.myTextField {


}
}

SwiftUI validate input in textfields

Try to validate what you want in the TextField onRecive method like this:

class TextValidator: ObservableObject {

@Published var text = ""

}

struct ContentView: View {

@ObservedObject var textValidator = TextValidator()
var body: some View {
TextField("Type Here", text: $textValidator.text)
.padding(.horizontal, 20.0)
.textFieldStyle(RoundedBorderTextFieldStyle())
.onReceive(Just(textValidator.text)) { newValue in
let value = newValue.replacingOccurrences(
of: "\\W", with: "", options: .regularExpression)
if value != newValue {
self.textValidator.text = value
}
print(newValue)
}
}
}

Swift validate 2 uitextfield password during text entry

Use textfield editing changed event not shouldChangeCharactersIn, because when you check textConfirmPIN.text!.count in shouldChangeCharactersIn the last typed char still not present yet until return true

that's why it called when you typed 7th character

Sample Image



Related Topics



Leave a reply



Submit