身份證號碼驗證規則 輸入身份證號碼:提交 代碼如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta h ...
身份證號碼驗證規則 輸入身份證號碼: 提交 代碼如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>身份證號碼驗證規則</title> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script> <script> $(function(){ $('.btn').click(function(){ let orginnumber =$('.number').val(); let partn = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;//先用正則判斷是否為正確的格式 if (!partn.exec(orginnumber)) { alert('請檢查你輸入的身份證個式是否正確!'); }else{ let number = [...orginnumber]; //es6語法,利用...將變數的值轉成數組。 let ration= new Array("7","9","10","5","8","4","2","1","6","3","7","9","10","5","8","4","2");//繫數 let ration22= new Array("1","0","X","9","8","7","6","5","4","3","2"); let sum=0; console.log(ration); for(let i=0;i<number.length-1;i++){ sum=number[i]*ration[i]+sum; console.log(i); } let mod = sum%11; if(ration22[mod]==number[17]){ alert('正確'); }else{ alert('不正確'); } } }) }) </script> </head> <body> <div> <lable for="input">輸入身份證號碼:</lable><input type="text" class="number"/><button class="btn">提交</button> </div> </body> </html>