PPS编号主要用于爱尔兰。它的验证包含更多的计算。我们如何验证swift代码中的PPS编号?。
以下代码用于验证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编号,返回布尔值编号是否有效。