1.php

<FORM METHOD='post' ACTION='2.php'>
<INPUT TYPE='test' NAME='id' value=''>
<input type=submit value='確認送出'>
</form>
2.php

<?php
//字母對照表
$char_list = array("A"=>10, "B"=>11, "C"=>12, "D"=>13, "E"=>14, "F"=>15, "G"=>16, "H"=>17, "I"=>34, "J"=>18, "K"=>19, "L"=>20, "M"=>21, "N"=>22, "O"=>35, "P"=>23, "Q"=>24, "R"=>25, "S"=>26, "T"=>27, "U"=>28, "V"=>29, "W"=>32, "X"=>30, "Y"=>31, "Z"=>33);
//計算對應之被乘數
$cal_lsit = "1987654321";

$pattern = "([A-Za-z]{1}[0-9]{9})";

$id = $_REQUEST['id'];
if (strlen($id)<>10){
echo "你輸入的身份證格式有誤,可能太多或太少!!<br>\n";
exit;
}elseif(substr($id,1,1)<>1 and substr($id,1,1)<>2){
echo "你輸入的身份證格式有誤!!<br>\n";
exit;
}elseif(!ereg($pattern,$id)){
echo "你輸入的身份證格式有誤!!<br>\n";
exit;
}

$check_id = ucfirst($id);

$cal_str = $char_list[substr($check_id,0,1)].substr($check_id,1,8);

for($i=0;$i<10;$i++){
$cal_num = $cal_num+substr($cal_str,$i,1)*substr($cal_lsit,$i,1);
}

$final_num = 10 - $cal_num%10;

if($final_num<>substr($check_id,9,1)){
echo "你輸入的身份證格式有誤,尾數應該是[".$final_num."]!!<br>\n";
exit;
}else{
echo "你輸入的身份證[".$check_id."]是正確的!!<br>\n";
}
?>

Last modified: Tuesday, 6 September 2011, 7:54 PM