Abdulaziz.Musaileekh
Abdulaziz.Musaileekh

Reputation: 325

Format UITextField MM/YY as expiration

Trying to have a textField for Credit Card expiry date the below code is working properly just looking to change the behavior, currently when you are typing from the third number / character will be added as an expiration format. What if I want to add the / character once user type the second number for example if the user typed 01 directly insert the separator character

open func reformatAsExpiration(_ textField: UITextField) {
        guard let string = textField.text else { return }
        let expirationString = String(ccrow.expirationSeparator)
        let cleanString = string.replacingOccurrences(of: expirationString, with: "", options: .literal, range: nil)
        if cleanString.length >= 3 {
            let monthString = cleanString[Range(0...1)]
            var yearString: String
            if cleanString.length == 3 {
                yearString = cleanString[2]
            } else {
                yearString = cleanString[Range(2...3)]
            }
            textField.text = monthString + expirationString + yearString
        } else {
            textField.text = cleanString
        }
    }

Upvotes: 3

Views: 7903

Answers (5)

hari sri
hari sri

Reputation: 11

Here is the full answer for Expiry date calculation from textfield formatted as MM/YY

var cleanString = string.replacingOccurrences(of: "/", with: "")

    if cleanString.rangeOfCharacter(from: unsupportedCharacterSet) != nil {
        return ""
    }
    
    let dateString: String
    
    if cleanString.count == 0 {
        return string
    } else if cleanString.count > 4 {
        // trim the string down to 4
        
        let reqIndex = cleanString.index(cleanString.startIndex, offsetBy: 4)
        cleanString = String(cleanString[..<reqIndex])
        
    }
    
    if cleanString.hasPrefix("0") == false && cleanString.hasPrefix("1") == false {
        dateString = "0" + cleanString
    } else {
        dateString = cleanString
    }
    let currentYear = Calendar.current.component(.year, from: Date()) % 100   // This will give you current year (i.e. if 2019 then it will be 19)
      let currentMonth = Calendar.current.component(.month, from: Date()) // This will give you current month (i.e if June then it will be 6)
    var newText = ""
    for (index, character) in dateString.enumerated() {
        print("index: \(index)")
        if index == 1 {
            let enterdMonth = Int(dateString.prefix(2)) ?? 0  // get first two digit from entered string as month
            print("enterdMonth at 1:\(enterdMonth)")
            if (1 ... 12).contains(enterdMonth){
                if enterdMonth < 10 {
                    newText = "0" + "\(enterdMonth)" + "/"
                }else {
                    newText = "\(enterdMonth)" + "/"
                }
            }else{
                
            }
        }else if index == 2 {
            if (2 ... 99).contains(Int(String(character))!) { // Entered year should be greater than 2019.
                newText.append(character)
            }else{
                
            }
        }else if index == 3 {
            print("index---: \(index)")
            let enterdYr = Int(dateString.suffix(2)) ?? 0   // get last two digit from entered string as year
            let enterdMonth = Int(dateString.prefix(2)) ?? 0  // get first two digit from entered string as month
            print("enterdYr: \(enterdYr)")
            print("currentYear: \(currentYear)")
            if (2 ... 99).contains(enterdYr) { // Entered year should be greater than 2019
                if enterdYr >= currentYear {
                    if (1 ... 12).contains(enterdMonth) {
                        if enterdMonth < 10 {
                            newText = "0" + "\(enterdMonth)" + "/" + "\(enterdYr)"
                        }else {
                            newText = "\(enterdMonth)" + "/" + "\(enterdYr)"
                        }
                        return newText
                    }
                }
            }
        }
        else {
            newText.append(character)
        }
    }
    return newText
}

Upvotes: 1

Saifan Nadaf
Saifan Nadaf

Reputation: 1775

Swift 5 :

Here is the solution with validation of entered month and year

Use TextField delegate method shouldChangeCharactersIn

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    guard let oldText = textField.text, let r = Range(range, in: oldText) else {
        return true
    }
    let updatedText = oldText.replacingCharacters(in: r, with: string)

    if string == "" {
        if updatedText.count == 2 {
            textField.text = "\(updatedText.prefix(1))"
            return false
        }
    } else if updatedText.count == 1 {
        if updatedText > "1" {
            return false
        }
    } else if updatedText.count == 2 {
        if updatedText <= "12" { //Prevent user to not enter month more than 12
            textField.text = "\(updatedText)/" //This will add "/" when user enters 2nd digit of month
        }
        return false
    } else if updatedText.count == 5 {
        self.expDateValidation(dateStr: updatedText)
    } else if updatedText.count > 5 {
        return false
    }

    return true
}

Validation logic in the following function

func expDateValidation(dateStr:String) {

    let currentYear = Calendar.current.component(.year, from: Date()) % 100   // This will give you current year (i.e. if 2019 then it will be 19)
    let currentMonth = Calendar.current.component(.month, from: Date()) // This will give you current month (i.e if June then it will be 6)

    let enteredYear = Int(dateStr.suffix(2)) ?? 0 // get last two digit from entered string as year
    let enteredMonth = Int(dateStr.prefix(2)) ?? 0 // get first two digit from entered string as month
    print(dateStr) // This is MM/YY Entered by user

    if enteredYear > currentYear {
        if (1 ... 12).contains(enteredMonth) {
            print("Entered Date Is Right")
        } else {
            print("Entered Date Is Wrong")
        }
    } else if currentYear == enteredYear {
        if enteredMonth >= currentMonth {
            if (1 ... 12).contains(enteredMonth) {
               print("Entered Date Is Right")
            } else {
               print("Entered Date Is Wrong")
            }
        } else {
            print("Entered Date Is Wrong")
        }
    } else {
       print("Entered Date Is Wrong")
    }

}

Upvotes: 12

Sebastian
Sebastian

Reputation: 1

Swift 5 solution to format date mm / yy:

First of all I added an observer to check text changes:

dateTextField.addTarget(self, action: #selector(expirationDateDidChange), for: .editingChanged)

This is the function to format text in real time:

@objc func expirationDateDidChange() {
    let currentText = dateTextField.text ?? ""

    // To remove " / " when user deletes the space after '/'
    if currentText.count == 4 {
        dateTextField.text = String(currentText.prefix(2))
        return
    }

    var dateText = dateTextField.text?.replacingOccurrences(of: "/", with: "") ?? ""
    dateText = dateText.replacingOccurrences(of: " ", with: "")

    var newText = ""
    for (index, character) in dateText.enumerated() {
        if index == 1 {
            newText = "\(newText)\(character) / "
        } else {
            newText.append(character)
        }
    }
    dateTextField.text = newText
}

Upvotes: 0

junaid
junaid

Reputation: 203

use this code in your textfield did begin editing for that textfield.

  let datePickerView = UIDatePicker()
                datePickerView.datePickerMode = .date                
                datePickerView.addTarget(self, action: #selector(handleChange(sender:)), for: .valueChanged)
                yourTextField.inputView = datePickerView

now implement this method for handle change in picker view

@objc func handleChange(sender: UIDatePicker){
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM/yyyy"
            yourTextfield.text = dateFormatter.string(from: sender.date)
      }

Hope it may help :)

Upvotes: 2

Anuraj
Anuraj

Reputation: 1240

You textfield delegate method shouldChangeCharactersIn to accomplish this

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

        if string == "" {
            return true
        }

        let currentText = textField.text! as NSString
        let updatedText = currentText.replacingCharacters(in: range, with: string)

        textField.text = updatedText
        let numberOfCharacters = updatedText.count
        if numberOfCharacters == 2 {
            textField.text?.append("/")
        }
        return false
    }

enter image description here

Upvotes: 2

Related Questions