将UITextField MM/YY格式设置为过期 [英] Format UITextField MM/YY as expiration
本文介绍了将UITextField MM/YY格式设置为过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用textField
作为信用卡的到期日期,以下代码仅在试图更改行为时正常工作,目前,当您从第三个数字/键入字符时,将添加为到期格式.如果我想在用户键入第二个数字后添加/字符怎么办,例如,如果用户键入01,则直接插入分隔符
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
}
}
推荐答案
迅速5:
这是验证输入的月份和年份的解决方案
Here is the solution with validation of entered month and year
使用TextField委托方法shouldChangeCharactersIn
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
}
以下功能中的验证逻辑
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")
}
}
这篇关于将UITextField MM/YY格式设置为过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文