1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| <!DOCTYPE html> <html> <head> <link href="https://code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" /> <script src="https://code.jquery.com/qunit/qunit-git.js"></script>
<meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <div id="qunit"></div> <div id="qunit-fixture"></div> </body> </html>
<script>
QUnit.module('測試身份證');
function getTwID(){ var city = new Array( 1,10,19,28,37,46,55,64,39,73,82, 2,11, 20,48,29,38,47,56,65,74,83,21, 3,12,30 ) var id = new Array(); id[0] = String.fromCharCode(Math.floor(Math.random() * (26)) + 65); id[1] = Math.floor(Math.random() * (2)) + 1; for(var i=2; i<9; i++){ id[i] = Math.floor(Math.random() * (9)) + 0; } var total = city[id[0].charCodeAt(0)-65]; for(var i=1; i<=8; i++){ total += eval(id[i]) * (9 - i); } var total_arr = (total+'').split(''); var lastChar = eval(10-total_arr[total_arr.length-1]); var lastChar_arr = (lastChar+'').split(''); id[id.length++] = lastChar_arr[lastChar_arr.length-1]; return id.join(''); }
function checkTwID(id){ var city = new Array( 1,10,19,28,37,46,55,64,39,73,82, 2,11, 20,48,29,38,47,56,65,74,83,21, 3,12,30 ) id = id.toUpperCase(); if (id.search(/^[A-Z](1|2)\d{8}$/i) == -1) { alert('基本格式錯誤'); return false; } else { id = id.split(''); var total = city[id[0].charCodeAt(0)-65]; for(var i=1; i<=8; i++){ total += eval(id[i]) * (9 - i); } total += eval(id[9]); return ((total%10 == 0 )); } }
var chartId_list = [];
for(var i = 0; i < 1000;i++){ chartId_list.push(getTwID()); }
for(let i = 0; i < 1000; i++){ QUnit.test( "驗證ID:" + chartId_list[i], function( assert ) { assert.ok( checkTwID(chartId_list[i]), " 網路方法 Passed!" ); }); } </script>
|