PPS 号码主要用于爱尔兰。它的验证包含更多的计算。我们如何在 swift 代码中验证 PPS 编号?
问问题
44 次
1 回答
0
下面的代码用于验证 iOS Swift 中的 PPS 编号
func isValidPPSN(_ ppsn: String) -> Bool {
let ppsnRegEx = "^([0-9]{7})([A-Z]{1,2})$"
let ppsnPred = NSPredicate(format:"SELF MATCHES %@", ppsnRegEx)
return ppsnPred.evaluate(with: ppsn)
}
func validatePPSN(objInputValue: String) -> Bool {
if !(isValidPPSN(objInputValue)) {
return false
}
else {
let numericPart = objInputValue[0 ..< 7]
let checksumCharacter = objInputValue[numericPart.count ..< objInputValue.count]
var multiplyingFactor = 8
var sum = 0
for i in numericPart {
sum += (Int(String(i)) ?? 0) * multiplyingFactor
multiplyingFactor -= 1
}
if checksumCharacter.count == 2 {
sum += (Int(Character(checksumCharacter[1].uppercased()).asciiValue ?? 0) - 64) * 9
}else{
return false
}
let checksum = sum % 23;
let asciiValue = Character(checksumCharacter[0].uppercased()).asciiValue ?? 0
if ((checksum + 64) == asciiValue || (checksum == 0 && asciiValue == 87)) {
return true
} else {
return false
}
}
}
调用函数validatePPSN(objInputValue: String)
在参数中传递 PPS 编号,其返回 bool 值编号是否有效。
于 2021-04-15T13:40:53.503 回答