Andre
Andre

Reputation: 7638

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

Evening, in my app I have several UITextfield. Each one has to confirm to different limitations.

For example, I have a date Field, zipCode Field, SSN Field etc.

From the Apple Documentation I found:

Assign a delegate object to handle important tasks, such as:

  • Determining whether the user should be allowed to edit the text field’s contents.

  • Validating the text entered by the user.

  • Responding to taps in the keyboard’s return button.

  • Forwarding the user-entered text to other parts of your app.

  • Store a reference to the text field in one of your controller objects.

So I'm pretty sure I have to use delegates and func textFieldDidEndEditing(_:).

The only way that came to my mind is to use a switch statement inside the func textFieldDidEndEditing(_:) to confirm the delegate to the difference limitation.

Is there a better, safer and faster pattern to face this problem?

Upvotes: 5

Views: 5120

Answers (4)

Raginmari
Raginmari

Reputation: 2529

If you need more control over the text which is committed to the text field or if you want to provide feedback while the text field is being edited, you should implement a different delegate instead:

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // Build and check the new text value. Accept or reject the change.
}

In the delegate, you can build the new text value. Validate it, apply constraints, decide on which feedback the user should receive ("Your password must be at least eight characters long", "This is not a valid IBAN"), accept or reject the change (i.e. return false).

Keep in mind, that the delegate is not called if you assign the text property manually. Moreover, this delegate is called when text is pasted into or deleted from the text field, which can make matters much more complicated depending on what you do.

Upvotes: 1

Farhad Faramarzi
Farhad Faramarzi

Reputation: 465

you can create enum for validation

enum Type {
   case zipcode
   case number
}

then you can create a method for validation like this :

func isValidate(text: String, type: Type) -> Bool {
    switch type {
    case .zipcode:
        ...
    }
}

this method can be in Util class. this is best practice. because your logic is encapsulate from out .

Upvotes: 1

Ketan Parmar
Ketan Parmar

Reputation: 27428

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 {


    }
}

Upvotes: 1

Annie Dev
Annie Dev

Reputation: 282

You are right, you will have to check the textfield, either you can check tags assigned for different text fields using switch statement like you said, or you can compare textfields itself,

if textfield1,textfield2 are outlets to two text fields, you can compare as following,

func textFieldDidEndEditing(textField: UITextField)
{
  if textField == textfield1
  {

  } 
  else if textField == textfield2
  {

  } 
}

Upvotes: 1

Related Questions