0

I have a field that takes a verification code sent through SMS. The code sent to all users is 4 digit numeric code. I want the user to type the code, and upon entering the 4th digit, the code is checked automatically (no button click required) and print correct to console if matches expected value. If not matching, then print Incorrect to console (console used here for printing purposes).

Assuming the code I have (fixed for purposes of testing) is 1234, below is my attempt that is not working as expected:

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

        if let text = textField.text {

            if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
                if(text.characters.count > 4) {
                    floatingLabelTextField.errorMessage = "code is invalid"
                }
                else {
                    // The error message will only disappear when we reset it to nil or empty string
                    floatingLabelTextField.errorMessage = ""
                    let smsInputCode = Int(self.verificationTextField.text!)
                    if(smsInputCode == self.smsSampleCode)
                    {
                        print("Correct Code")
                    }else{
                        print("Incorrect Code")
                    }
                }
            }
        }
        return true
    }

The above code prints "incorrect" 4 times as I am typing the digits then shows correct code only after I click on tab bar on keyboard (not auto detection).

Appreciate your help.

Hugo Tunius
  • 2,869
  • 24
  • 32
ksjg
  • 5
  • 3
  • @Khalid i guess you type 1 in text field it checks 1==1234 prints incorrect then same for 2=1234 print incorrect ,3=1234 again incorrect then 4=1234 again incorrect and then exit out .Then at last reads 1234=1234 gives you correct when you click tab bar. – Tushar Sharma Feb 18 '17 at 05:11
  • @sasquatch If that is done, how would that help with correct and incorrect statements? – ksjg Feb 18 '17 at 05:13
  • @Ksjg Instead of (else) you can use else if text.character.count==4 . – Tushar Sharma Feb 18 '17 at 05:31
  • @TusharSharma That doesn't solve it ...same behavior persists – ksjg Feb 18 '17 at 05:34
  • @ksjg. have u tried my demo? – aircraft Feb 19 '17 at 01:05
  • @aircraft sorry got held back by another bug. Fixing it and then trying this code. if it works, will surely select it here as answer – ksjg Feb 19 '17 at 14:49

2 Answers2

0

Try this:

Step - 1
UITextField action

yourtextfieldname.addTarget(self, action: #selector(self.textFieldEditingChange(_:)), for: UIControlEvents.editingChanged)

Step - 2
UITextField method

func textFieldEditingChange(_ textField:UITextField) {
        //self.columnValue[textField.tag] = textField.text!
        if let text = textField.text {
            if let floatingLabelTextField = textField as? SkyFloatingLabelTextField {
                if(text.characters.count > 4) {
                    floatingLabelTextField.errorMessage = "code is invalid"
            }
            else {
                // The error message will only disappear when we reset it to nil or empty string
                floatingLabelTextField.errorMessage = ""
                let smsInputCode = Int(text)
                    if(smsInputCode == self.smsSampleCode)
                    {
                        print("Correct Code")
                    }else{
                        print("Incorrect Code")
                    }
                }
            }
        }
    }
iParesh
  • 2,338
  • 1
  • 18
  • 30
0

I write a demo you can refer to:

In the ViewController:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    @IBOutlet weak var label: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        textField.addTarget(self, action: #selector(textFieldAction), for: .editingChanged)


    }

    func textFieldAction() {

        if (textField.text?.characters.count)! > 4 {

            let lengh = textField.text?.characters.count

            let str:NSString = textField.text! as NSString

            textField.text = str.substring(to: lengh! - 1)
        }

        if textField.text == "1234" {
            label.text = "valid"
        }else {
            label.text = "invalid"
        }

    }

}

The result:

enter image description here

aircraft
  • 25,146
  • 28
  • 91
  • 166