title: Swift -How to put multiple characters in each individual SMS Otp UITextfield
body: <p>I'm trying to input user emirates id in this format <a href="https://i.stack.imgur.com/ZuDpJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZuDpJ.png" alt="enter image description here"></a></p>
  
  <p>for this i try and follow </p>
  
  <p><a href="https://stackoverflow.com/a/54601324/428240">https://stackoverflow.com/a/54601324/428240</a></p>
  
  <p>its working fine if i have one character but if you check the image, i want first textfield to show 3 characters, second to show 4 , third to show 7 and fourth textfield to show 1 character. and also if i try to remove character from textfield it should not remove all instead only remove one by one and then move to other once there is noting in textfield.</p>
  
  <p>can any one help me with this. thanks in advance </p>
  
  <pre><code>protocol EmiratesIdTextFieldDelegate: class {
      func textFieldDidDelete()
  }
  
  import UIKit
  
  class EmiratesIdTextField: UITextField {
  
  weak var emiratesIdTextFieldDelegate: EmiratesIdTextFieldDelegate? // make sure to declare this as weak to prevent a memory leak/retain cycle
  
  override func deleteBackward() {
      super.deleteBackward()
      emiratesIdTextFieldDelegate?.textFieldDidDelete()
  }
  
  // when a char is inside the textField this keeps the cursor to the right of it. If the user can get on the left side of the char and press the backspace the current char won't get deleted
  override func closestPosition(to point: CGPoint) -&gt; UITextPosition? {
      let beginning = self.beginningOfDocument
      let end = self.position(from: beginning, offset: self.text?.count ?? 0)
      return end
  }
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>class PinViewController: UIViewController,EmiratesIdTextFieldDelegate,UITextFieldDelegate {
  @IBOutlet weak var textField1: EmiratesIdTextField!
  @IBOutlet weak var textField2: EmiratesIdTextField!
  @IBOutlet weak var textField3: EmiratesIdTextField!
  @IBOutlet weak var textField4: EmiratesIdTextField!
  
  var activeTextField = UITextField()
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      textField1.delegate = self
      textField2.delegate = self
      textField3.delegate = self
      textField4.delegate = self
  
      textField1.emiratesIdTextFieldDelegate = self
      textField2.emiratesIdTextFieldDelegate = self
      textField3.emiratesIdTextFieldDelegate = self
      textField4.emiratesIdTextFieldDelegate = self
  
  //  configureAnchors()
  
      textField1.becomeFirstResponder()
  }
  
  func textFieldDidBeginEditing(_ textField: UITextField) {
  
      activeTextField = textField
  }
  
  func textFieldDidDelete() {
  
      if activeTextField == textField1 {
          print("backButton was pressed in otpTextField1")
          // do nothing
      }
  
      if activeTextField == textField2 {
          print("backButton was pressed in otpTextField2")
          textField2.isEnabled = false
          textField1.isEnabled = true
          textField1.becomeFirstResponder()
          textField1.text = ""
      }
  
      if activeTextField == textField3 {
          print("backButton was pressed in otpTextField3")
          textField3.isEnabled = false
          textField2.isEnabled = true
          textField2.becomeFirstResponder()
          textField2.text = ""
      }
  
      if activeTextField == textField4 {
          print("backButton was pressed in otpTextField4")
          textField4.isEnabled = false
          textField3.isEnabled = true
          textField3.becomeFirstResponder()
          textField3.text = ""
      }
  }
  
  func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -&gt; Bool {
  
      let text = textField.text
  
      if let text = text {
  
          // 10. when the user enters something in the first textField it will automatically adjust to the next textField and in the process do some disabling and enabling. This will proceed until the last textField
          if (text.count &lt; 1) &amp;&amp; (string.count &gt; 0) {
  
              if textField == textField1 {
                  textField1.isEnabled = false
                  textField2.isEnabled = true
                  textField2.becomeFirstResponder()
              }
  
              if textField == textField2 {
                  textField2.isEnabled = false
                  textField3.isEnabled = true
                  textField3.becomeFirstResponder()
              }
  
              if textField == textField3 {
                  textField3.isEnabled = false
                  textField4.isEnabled = true
                  textField4.becomeFirstResponder()
              }
  
              if textField == textField4 {
                  // do nothing
              }
  
              textField.text = string
              return false
  
          } // 11. if the user gets to the last textField and presses the back button everything above will get reversed
          else if (text.count &gt;= 1) &amp;&amp; (string.count == 0) {
  
              if textField == textField2 {
                  textField2.isEnabled = false
                  textField2.isEnabled = true
                  textField1.becomeFirstResponder()
                  textField1.text = ""
              }
  
              if textField == textField3 {
                  textField3.isEnabled = false
                  textField2.isEnabled = true
                  textField2.becomeFirstResponder()
                  textField2.text = ""
              }
  
              if textField == textField4 {
                  textField4.isEnabled = false
                  textField3.isEnabled = true
                  textField3.becomeFirstResponder()
                  textField3.text = ""
              }
  
              if textField == textField1 {
                  // do nothing
              }
  
              textField.text = ""
              return false
  
          } // 12. after pressing the backButton and moving forward again you will have to do what's in step 10 all over again
          else if text.count &gt;= 1 {
  
              if textField == textField1 {
                  textField1.isEnabled = false
                  textField2.isEnabled = true
                  textField2.becomeFirstResponder()
              }
  
              if textField == textField2 {
                  textField2.isEnabled = false
                  textField3.isEnabled = true
                  textField3.becomeFirstResponder()
              }
  
              if textField == textField3 {
                  textField3.isEnabled = false
                  textField4.isEnabled = true
                  textField4.becomeFirstResponder()
              }
  
              if textField == textField4 {
                  // do nothing
              }
  
              textField.text = string
              return false
          }
      }
      return true
  }
  }
  </code></pre>