cnblogs/dcrenl/大陆、港澳台身份证、护照、军官证的正则表达式.html
2024-09-24 12:43:01 +08:00

114 lines
27 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<div>
<div>
<p>最近工作因为工作需要,有一个身份验证需要前端验证<strong>大陆、港澳台身份证、护照、军官证</strong>号码的合法性。<br />初步整理了一下各个验证的正则表达式,如有错误请大家指出,谢谢!</p>
<h2>大陆身份证正则表达式:</h2>
<div class="_2Uzcx_"><button class="VJbwyy" type="button"></button>
<pre class="line-numbers language-jsx"><code class=" language-jsx"><span class="token function">idCardValid<span class="token punctuation">(<span class="token parameter">id<span class="token punctuation">) <span class="token punctuation">{
<span class="token comment">// 1 "验证通过!", 0 //校验不通过
<span class="token keyword">var format <span class="token operator">= <span class="token regex">/^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/<span class="token punctuation">;
<span class="token comment">//号码规则校验
<span class="token keyword">if <span class="token punctuation">(<span class="token operator">!format<span class="token punctuation">.<span class="token function">test<span class="token punctuation">(id<span class="token punctuation">)<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token punctuation">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token punctuation">: <span class="token string">'身份证号码不合规' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">}
<span class="token comment">//区位码校验
<span class="token comment">//出生年月日校验 前正则限制起始年份为1900;
<span class="token keyword">var year <span class="token operator">= id<span class="token punctuation">.<span class="token function">substr<span class="token punctuation">(<span class="token number">6<span class="token punctuation">, <span class="token number">4<span class="token punctuation">)<span class="token punctuation">,<span class="token comment">//身份证年
month <span class="token operator">= id<span class="token punctuation">.<span class="token function">substr<span class="token punctuation">(<span class="token number">10<span class="token punctuation">, <span class="token number">2<span class="token punctuation">)<span class="token punctuation">,<span class="token comment">//身份证月
date <span class="token operator">= id<span class="token punctuation">.<span class="token function">substr<span class="token punctuation">(<span class="token number">12<span class="token punctuation">, <span class="token number">2<span class="token punctuation">)<span class="token punctuation">,<span class="token comment">//身份证日
time <span class="token operator">= Date<span class="token punctuation">.<span class="token function">parse<span class="token punctuation">(month <span class="token operator">+ <span class="token string">'-' <span class="token operator">+ date <span class="token operator">+ <span class="token string">'-' <span class="token operator">+ year<span class="token punctuation">)<span class="token punctuation">,<span class="token comment">//身份证日期时间戳date
now_time <span class="token operator">= Date<span class="token punctuation">.<span class="token function">parse<span class="token punctuation">(<span class="token keyword">new <span class="token class-name">Date<span class="token punctuation">(<span class="token punctuation">)<span class="token punctuation">)<span class="token punctuation">,<span class="token comment">//当前时间戳
dates <span class="token operator">= <span class="token punctuation">(<span class="token keyword">new <span class="token class-name">Date<span class="token punctuation">(year<span class="token punctuation">, month<span class="token punctuation">, <span class="token number">0<span class="token punctuation">)<span class="token punctuation">)<span class="token punctuation">.<span class="token function">getDate<span class="token punctuation">(<span class="token punctuation">)<span class="token punctuation">;<span class="token comment">//身份证当月天数
<span class="token keyword">if <span class="token punctuation">(time <span class="token operator">&gt; now_time <span class="token operator">|| date <span class="token operator">&gt; dates<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token punctuation">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token punctuation">: <span class="token string">'出生日期不合规' <span class="token punctuation">}
<span class="token punctuation">}
<span class="token comment">//校验码判断
<span class="token keyword">var c <span class="token operator">= <span class="token keyword">new <span class="token class-name">Array<span class="token punctuation">(<span class="token number">7<span class="token punctuation">, <span class="token number">9<span class="token punctuation">, <span class="token number">10<span class="token punctuation">, <span class="token number">5<span class="token punctuation">, <span class="token number">8<span class="token punctuation">, <span class="token number">4<span class="token punctuation">, <span class="token number">2<span class="token punctuation">, <span class="token number">1<span class="token punctuation">, <span class="token number">6<span class="token punctuation">, <span class="token number">3<span class="token punctuation">, <span class="token number">7<span class="token punctuation">, <span class="token number">9<span class="token punctuation">, <span class="token number">10<span class="token punctuation">, <span class="token number">5<span class="token punctuation">, <span class="token number">8<span class="token punctuation">, <span class="token number">4<span class="token punctuation">, <span class="token number">2<span class="token punctuation">)<span class="token punctuation">; <span class="token comment">//系数
<span class="token keyword">var b <span class="token operator">= <span class="token keyword">new <span class="token class-name">Array<span class="token punctuation">(<span class="token string">'1'<span class="token punctuation">, <span class="token string">'0'<span class="token punctuation">, <span class="token string">'X'<span class="token punctuation">, <span class="token string">'9'<span class="token punctuation">, <span class="token string">'8'<span class="token punctuation">, <span class="token string">'7'<span class="token punctuation">, <span class="token string">'6'<span class="token punctuation">, <span class="token string">'5'<span class="token punctuation">, <span class="token string">'4'<span class="token punctuation">, <span class="token string">'3'<span class="token punctuation">, <span class="token string">'2'<span class="token punctuation">)<span class="token punctuation">; <span class="token comment">//校验码对照表
<span class="token keyword">var id_array <span class="token operator">= id<span class="token punctuation">.<span class="token function">split<span class="token punctuation">(<span class="token string">""<span class="token punctuation">)<span class="token punctuation">;
<span class="token keyword">var sum <span class="token operator">= <span class="token number">0<span class="token punctuation">;
<span class="token keyword">for <span class="token punctuation">(<span class="token keyword">var k <span class="token operator">= <span class="token number">0<span class="token punctuation">; k <span class="token operator">&lt; <span class="token number">17<span class="token punctuation">; k<span class="token operator">++<span class="token punctuation">) <span class="token punctuation">{
sum <span class="token operator">+= <span class="token function">parseInt<span class="token punctuation">(id_array<span class="token punctuation">[k<span class="token punctuation">]<span class="token punctuation">) <span class="token operator">* <span class="token function">parseInt<span class="token punctuation">(c<span class="token punctuation">[k<span class="token punctuation">]<span class="token punctuation">)<span class="token punctuation">;
<span class="token punctuation">}
<span class="token keyword">if <span class="token punctuation">(id_array<span class="token punctuation">[<span class="token number">17<span class="token punctuation">]<span class="token punctuation">.<span class="token function">toUpperCase<span class="token punctuation">(<span class="token punctuation">) <span class="token operator">!= b<span class="token punctuation">[sum <span class="token operator">% <span class="token number">11<span class="token punctuation">]<span class="token punctuation">.<span class="token function">toUpperCase<span class="token punctuation">(<span class="token punctuation">)<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token punctuation">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token punctuation">: <span class="token string">'身份证校验码不合规' <span class="token punctuation">}
<span class="token punctuation">}
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token punctuation">: <span class="token number">1<span class="token punctuation">, <span class="token string">'msg'<span class="token punctuation">: <span class="token string">'校验通过' <span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">,
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
<h2>港澳身份证正则表达式:</h2>
<div class="_2Uzcx_"><button class="VJbwyy" type="button"></button>
<pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token function">isHKCard<span class="token punctuation">(card<span class="token punctuation">) <span class="token punctuation">{
<span class="token comment">// 港澳居民来往内地通行证
<span class="token comment">// 规则: H/M + 10位或6位数字
<span class="token comment">// 样本: H1234567890
<span class="token keyword">var reg <span class="token operator">= <span class="token operator">/^<span class="token punctuation">(<span class="token punctuation">[A<span class="token operator">-Z<span class="token punctuation">]\d<span class="token punctuation">{<span class="token number">6<span class="token punctuation">,<span class="token number">10<span class="token punctuation">}<span class="token punctuation">(\<span class="token punctuation">(\w<span class="token punctuation">{<span class="token number">1<span class="token punctuation">}\<span class="token punctuation">)<span class="token punctuation">)<span class="token operator">?<span class="token punctuation">)$<span class="token operator">/<span class="token punctuation">;
<span class="token keyword">if <span class="token punctuation">(reg<span class="token punctuation">.<span class="token function">test<span class="token punctuation">(card<span class="token punctuation">) <span class="token operator">=== <span class="token boolean">false<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'港澳居民来往内地通行证号码不合规' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">} <span class="token keyword">else <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">1<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'校验通过' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">,
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
<h2>台湾身份证正则表达式:</h2>
<div class="_2Uzcx_"><button class="VJbwyy" type="button"></button>
<pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token function">isTWCard<span class="token punctuation">(card<span class="token punctuation">) <span class="token punctuation">{
<span class="token comment">// 台湾居民来往大陆通行证
<span class="token comment">// 规则: 新版8位或18位数字 旧版10位数字 + 英文字母
<span class="token comment">// 样本: 12345678 或 1234567890B
<span class="token keyword">var reg <span class="token operator">= <span class="token operator">/^\d<span class="token punctuation">{<span class="token number">8<span class="token punctuation">}|^<span class="token punctuation">[a<span class="token operator">-zA<span class="token operator">-Z0<span class="token operator">-<span class="token number">9<span class="token punctuation">]<span class="token punctuation">{<span class="token number">10<span class="token punctuation">}|^\d<span class="token punctuation">{<span class="token number">18<span class="token punctuation">}$<span class="token operator">/<span class="token punctuation">;
<span class="token keyword">if <span class="token punctuation">(reg<span class="token punctuation">.<span class="token function">test<span class="token punctuation">(card<span class="token punctuation">) <span class="token operator">=== <span class="token boolean">false<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'台湾居民来往大陆通行证号码不合规' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">} <span class="token keyword">else <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">1<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'校验通过' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">,
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
<h2>护照正则表达式:</h2>
<div class="_2Uzcx_"><button class="VJbwyy" type="button"></button>
<pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token function">isPassPortCard<span class="token punctuation">(card<span class="token punctuation">) <span class="token punctuation">{
<span class="token comment">// 护照
<span class="token comment">// 规则: 14/15开头 + 7位数字, G + 8位数字, P + 7位数字, S/D + 7或8位数字,等
<span class="token comment">// 样本: 141234567, G12345678, P1234567
<span class="token keyword">var reg <span class="token operator">= <span class="token operator">/^<span class="token punctuation">(<span class="token punctuation">[a<span class="token operator">-zA<span class="token operator">-z<span class="token punctuation">]|<span class="token punctuation">[<span class="token number">0<span class="token operator">-<span class="token number">9<span class="token punctuation">]<span class="token punctuation">)<span class="token punctuation">{<span class="token number">5<span class="token punctuation">,<span class="token number">17<span class="token punctuation">}$<span class="token operator">/<span class="token punctuation">;
<span class="token keyword">if <span class="token punctuation">(reg<span class="token punctuation">.<span class="token function">test<span class="token punctuation">(card<span class="token punctuation">) <span class="token operator">=== <span class="token boolean">false<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'护照号码不合规' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">} <span class="token keyword">else <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">1<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'校验通过' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">,
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
<h2>军官证正则表达式:</h2>
<div class="_2Uzcx_"><button class="VJbwyy" type="button"></button>
<pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token function">isOfficerCard<span class="token punctuation">(card<span class="token punctuation">) <span class="token punctuation">{
<span class="token comment">// 军官证
<span class="token comment">// 规则: 军/兵/士/文/职/广/(其他中文) + "字第" + 4到8位字母或数字 + "号"
<span class="token comment">// 样本: 军字第2001988号, 士字第P011816X号
<span class="token keyword">var reg <span class="token operator">= <span class="token operator">/^<span class="token punctuation">[\u4E00<span class="token operator">-\u9FA5<span class="token punctuation">]<span class="token punctuation">(字第<span class="token punctuation">)<span class="token punctuation">(<span class="token punctuation">[<span class="token number">0<span class="token operator">-9a<span class="token operator">-zA<span class="token operator">-Z<span class="token punctuation">]<span class="token punctuation">{<span class="token number">4<span class="token punctuation">,<span class="token number">8<span class="token punctuation">}<span class="token punctuation">)<span class="token punctuation">(号<span class="token operator">?<span class="token punctuation">)$<span class="token operator">/<span class="token punctuation">;
<span class="token keyword">if <span class="token punctuation">(reg<span class="token punctuation">.<span class="token function">test<span class="token punctuation">(card<span class="token punctuation">) <span class="token operator">=== <span class="token boolean">false<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'军官证号不合规' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">} <span class="token keyword">else <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">1<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'校验通过' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">}
<span class="token punctuation">}<span class="token punctuation">,
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
<h2>户口本正则表达式:</h2>
<div class="_2Uzcx_"><button class="VJbwyy" type="button"></button>
<pre class="line-numbers language-kotlin"><code class=" language-kotlin"><span class="token function">isAccountCard<span class="token punctuation">(card<span class="token punctuation">) <span class="token punctuation">{
<span class="token comment">// 户口本
<span class="token comment">// 规则: 15位数字, 18位数字, 17位数字 + X
<span class="token comment">// 样本: 441421999707223115
<span class="token keyword">var reg <span class="token operator">= <span class="token operator">/<span class="token punctuation">(^\d<span class="token punctuation">{<span class="token number">15<span class="token punctuation">}$<span class="token punctuation">)|<span class="token punctuation">(^\d<span class="token punctuation">{<span class="token number">18<span class="token punctuation">}$<span class="token punctuation">)|<span class="token punctuation">(^\d<span class="token punctuation">{<span class="token number">17<span class="token punctuation">}<span class="token punctuation">(\d|X|x<span class="token punctuation">)$<span class="token punctuation">)<span class="token operator">/<span class="token punctuation">;
<span class="token keyword">if <span class="token punctuation">(reg<span class="token punctuation">.<span class="token function">test<span class="token punctuation">(card<span class="token punctuation">) <span class="token operator">=== <span class="token boolean">false<span class="token punctuation">) <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">0<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'户口本号码不合规' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">} <span class="token keyword">else <span class="token punctuation">{
<span class="token keyword">return <span class="token punctuation">{ <span class="token string">'status'<span class="token operator">: <span class="token number">1<span class="token punctuation">, <span class="token string">'msg'<span class="token operator">: <span class="token string">'校验通过' <span class="token punctuation">}<span class="token punctuation">;
<span class="token punctuation">}
<span class="token punctuation">}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre>
</div>
</div>
<br /><br />作者:萧雪圣<br />链接https://www.jianshu.com/p/8d8eba87a048<br />来源:简书<br />著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</div>