正規表示式 判斷身份證號

2021-07-24 15:45:31 字數 3266 閱讀 4462

/**

**  判斷身份證是否正確

**  @param value 身份證號

**  @return yes or no

*/+ (bool

)judgeidcardlegal:(

nsstring

*)value;  

+ (bool

)judgeidcardlegal:(

nsstring

*)value else

}  // 省份**

nsarray

*areasarray =@[

@"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"

];  

nsstring

*valuestart

2= [value

substringtoindex:2

];  

bool

areaflag =

no;  

for(

nsstring

*areacode in areasarray)   

}  if

(!areaflag)   

//生日部分的編碼

nsregularexpression

*regularexpression;  

nsuinteger numberofmatch;  

nsinteger year =0

;  switch

(length) [0-9]((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]$"

options

:nsregularexpressioncaseinsensitive  

error

:nil

];//測試出生日期的合法性

}else

[0-9]((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]$"

options

:nsregularexpressioncaseinsensitive  

error

:nil

];//測試出生日期的合法性

}  //numberofmatch:匹配到得字串的個數

numberofmatch = [regularexpressionnumberofmatchesinstring

:value  

options

:nsmatchingreportprogress  

range

:nsmakerange(

0, value

.length

)];  

if(numberofmatch >

0) else

case

18:  

year = [valuesubstringwithrange

:nsmakerange(6,

4)].intvalue

;  if

(year %40

0==0|| (year %10

0!=0&& year %4==

0)) 19[0-9]((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][0-9xx]$"

regularexpression = [[nsregularexpressionalloc

]initwithpattern:

@"^[1-9][0-9](19|20)[0-9]((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][0-9xx]$"

options

:nsregularexpressioncaseinsensitive  

error

:nil

];//測試出生日期的合法性

}else

(19|20)[0-9]((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][0-9xx]$"

options

:nsregularexpressioncaseinsensitive  

error

:nil

];//測試出生日期的合法性

}  numberofmatch = [regularexpressionnumberofmatchesinstring

:value  

options

:nsmatchingreportprogress  

range

:nsmakerange(

0, value

.length

)];  

//驗證校驗位

if(numberofmatch >

0) else

}else

default

:  return

false

;  }  

}  

正規表示式之身份證號驗證

分析 身份證的號碼結構為 身份證前六位 表示編碼物件常住戶口所在縣 市 旗 區 的行政區劃 身份證第七位到第十四位 表示編碼物件出生的年 月 日,其中年份用四位數字表示,年 月 日之間不用分隔符。例如 1981年05月11日就用19810511表示。身份證第十五位到十七位 為同一位址碼所標識的區域範...

用正規表示式驗證身份證號

用正規表示式驗證身份證號 開發工具與關鍵技術 vs mvc在現在的資訊時代,個人的資訊真假難辨,但身份證號就是唯一的,也正因如此我們的資訊錄入都離不開身份證的使用。有了身份證的廣泛使用,同時驗證身份證號碼的知識也被廣泛應用,而我現在要說的就是用正規表示式驗證身份證號碼。在這裡我會以乙個包含新增身份證...

2014 11 2 有關身份證號的正規表示式

大陸的居民身份證號碼有兩種 18位和15位,15位的身份證號碼是老一代身份證號碼。18位和15位的區別在於兩個部分 第一,18位號碼的年份以4位計而15位號碼的年份為2位,如1987年在18位號碼中為 1987 而在15位號碼中為 87 這裡1987只是作為乙個例子可能1987年及以後根本不存在15...