gpt4 book ai didi

ios - 验证信用卡号

转载 作者:行者123 更新时间:2023-12-02 04:28:23 24 4
gpt4 key购买 nike

我正在将用户的信用卡号作为输入。我想允许 16 个数字,并希望在每 4 个数字后面格式化空格。为此,我做了以下工作。

-(void)cardNumberValidation:(id)sender{
UITextField *temp=sender;

if ([temp.text length]>19) {
txtCard.text= [temp.text substringToIndex:[temp.text length] - 1];
}
if ([temp.text length]==4) {
txtCard.text=[NSString stringWithFormat:@"%@ ",temp.text];

}
if ([temp.text length]==9) {
txtCard.text=[NSString stringWithFormat:@"%@ ",temp.text];
}
if ([temp.text length]==14) {
NSString *lastChar = [txtCard.text substringFromIndex:[txtCard.text length] - 1];
txtCard.text=[NSString stringWithFormat:@"%@ ",temp.text];
}
}

但是现在,当用户在输入所有字符后删除字符时,如果它达到 15 个字符,然后用户将再删除一个字符,那么此代码将在其后面添加空格,并且不允许进一步删除它。

任何人都可以给我建议或任何更好的方法来做到这一点。

最佳答案

在 swift

enum CardType: String {
case Unknown, Amex, Visa, MasterCard, Diners, Discover, JCB, Elo, Hipercard, UnionPay

static let allCards = [Amex, Visa, MasterCard, Diners, Discover, JCB, Elo, Hipercard, UnionPay]

var regex : String {
switch self {
case .Amex:
return "^3[47][0-9]{5,}$"
case .Visa:
return "^4[0-9]{6,}([0-9]{3})?$"
case .MasterCard:
return "^(5[1-5][0-9]{4}|677189)[0-9]{5,}$"
case .Diners:
return "^3(?:0[0-5]|[68][0-9])[0-9]{4,}$"
case .Discover:
return "^6(?:011|5[0-9]{2})[0-9]{3,}$"
case .JCB:
return "^(?:2131|1800|35[0-9]{3})[0-9]{3,}$"
case .UnionPay:
return "^(62|88)[0-9]{5,}$"
case .Hipercard:
return "^(606282|3841)[0-9]{5,}$"
case .Elo:
return "^((((636368)|(438935)|(504175)|(451416)|(636297))[0-9]{0,10})|((5067)|(4576)|(4011))[0-9]{0,12})$"
default:
return ""
}
}
}
extension UITextField{

func validateCreditCardFormat()-> (type: CardType, valid: Bool) {
// Get only numbers from the input string
var input = self.text!
let numberOnly = input.stringByReplacingOccurrencesOfString("[^0-9]", withString: "", options: .RegularExpressionSearch)

var type: CardType = .Unknown
var formatted = ""
var valid = false

// detect card type
for card in CardType.allCards {
if (matchesRegex(card.regex, text: numberOnly)) {
type = card
break
}
}

// check validity
valid = luhnCheck(numberOnly)

// format
var formatted4 = ""
for character in numberOnly.characters {
if formatted4.characters.count == 4 {
formatted += formatted4 + " "
formatted4 = ""
}
formatted4.append(character)
}

formatted += formatted4 // the rest

// return the tuple
return (type, valid)
}

func matchesRegex(regex: String!, text: String!) -> Bool {
do {
let regex = try NSRegularExpression(pattern: regex, options: [.CaseInsensitive])
let nsString = text as NSString
let match = regex.firstMatchInString(text, options: [], range: NSMakeRange(0, nsString.length))
return (match != nil)
} catch {
return false
}
}

func luhnCheck(number: String) -> Bool {
var sum = 0
let digitStrings = number.characters.reverse().map { String($0) }

for tuple in digitStrings.enumerate() {
guard let digit = Int(tuple.element) else { return false }
let odd = tuple.index % 2 == 1

switch (odd, digit) {
case (true, 9):
sum += 9
case (true, 0...8):
sum += (digit * 2) % 9
default:
sum += digit
}
}

return sum % 10 == 0
}
}

更多表格请前往http://kalapun.com/posts/card-checking-in-swift/

关于ios - 验证信用卡号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23888682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com