swift 인증 번호 입력의 합법성 검증
바로 코드!
func validateIDCardNumber(sfz:String)->Bool{
let value = sfz.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
var length = 0
if value == ""{
return false
}else{
length = value.characters.count
if length != 15 && length != 18{
return false
}
}
//
let arearsArray = ["11","12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "82", "91"]
let valueStart2 = (value as NSString).substringToIndex(2)
var arareFlag = false
if arearsArray.contains(valueStart2){
arareFlag = true
}
if !arareFlag{
return false
}
var regularExpression = NSRegularExpression()
var numberofMatch = Int()
var year = 0
switch (length){
case 15:
year = Int((value as NSString).substringWithRange(NSRange(location:6,length:2)))!
if year%4 == 0 || (year%100 == 0 && year%4 == 0){
do{
regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$", options: .CaseInsensitive) //
}catch{
}
}else{
do{
regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$", options: .CaseInsensitive) //
}catch{}
}
numberofMatch = regularExpression.numberOfMatchesInString(value, options:NSMatchingOptions.ReportProgress, range: NSMakeRange(0, value.characters.count))
if(numberofMatch > 0) {
return true
}else {
return false
}
case 18:
year = Int((value as NSString).substringWithRange(NSRange(location:6,length:4)))!
if year%4 == 0 || (year%100 == 0 && year%4 == 0){
do{
regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$", options: .CaseInsensitive) //
}catch{
}
}else{
do{
regularExpression = try NSRegularExpression.init(pattern: "^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$", options: .CaseInsensitive) //
}catch{}
}
numberofMatch = regularExpression.numberOfMatchesInString(value, options:NSMatchingOptions.ReportProgress, range: NSMakeRange(0, value.characters.count))
if(numberofMatch > 0) {
let s =
(Int((value as NSString).substringWithRange(NSRange(location:0,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:10,length:1)))!) * 7 +
(Int((value as NSString).substringWithRange(NSRange(location:1,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:11,length:1)))!) * 9 +
(Int((value as NSString).substringWithRange(NSRange(location:2,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:12,length:1)))!) * 10 +
(Int((value as NSString).substringWithRange(NSRange(location:3,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:13,length:1)))!) * 5 +
(Int((value as NSString).substringWithRange(NSRange(location:4,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:14,length:1)))!) * 8 +
(Int((value as NSString).substringWithRange(NSRange(location:5,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:15,length:1)))!) * 4 +
(Int((value as NSString).substringWithRange(NSRange(location:6,length:1)))! +
Int((value as NSString).substringWithRange(NSRange(location:16,length:1)))!) * 2 +
Int((value as NSString).substringWithRange(NSRange(location:7,length:1)))! * 1 +
Int((value as NSString).substringWithRange(NSRange(location:8,length:1)))! * 6 +
Int((value as NSString).substringWithRange(NSRange(location:9,length:1)))! * 3
let Y = s%11
var M = "F"
let JYM = "10X98765432"
M = (JYM as NSString).substringWithRange(NSRange(location:Y,length:1))
if M == (value as NSString).substringWithRange(NSRange(location:17,length:1))
{
return true
}else{return false}
}else {
return false
}
default:
return false
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
백그라운드에서 값을 계산하고 Swift 동시성 이후에 결과 사용값을 계산해야 하고 메인 스레드를 차단하지 않으려면 계산된 값을 반환하는 Swift Task 구조에서 해당 값을 계산하면 됩니다. Swift 동시성 이전에는 백그라운드 대기열로 이동하여 필요한 값을 계산하고 필요한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.