請幫我看一下下面身分證認證程式(Javascript, Ajax)

edited 十月 2013 in Javascript
不知如何會一直出現"必須要有物件"error message

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml">;
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5" />
<title>無標題文件</title>
</head>

<body>
<script type="text/javascript">

//Ajax
function sendRequest(){
var req = getNewRequest();
var handler = function(){
if(req.readyState == 4 && req.status == 200){
var msg = req.responseText;
if(msg == 1){
sMsg = "這個身分證已經有人註冊";
return sMsg;
}else{
sMsg = "";
return sMsg;
}
}
}
if(req){
req.open("GET","check_userid.php?user_id="+escape(sPID),true);
req.send(null);
req.onreadystatechange = handler;
}
}
function getNewRequest(){
var req = null;
if(window.XMLHttpRequest)
req = new XMLHttpRequest();
else
req = new ActiveXObject("Microsoft.XMLHTTP");
return req;
}

window.onload = sendRequest;
//
//身份證字號檢查器

var ALP_STR = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
var NUM_STR = "0123456789";
var SEX_STR = "12";
var MAX_COUNT = 999;

//
// 去字串前後空白
function trim(str) {
while (str.indexOf(" ")==0) {
str = str.substring(1, str.length);
}
while ((str.length>0) && (str.indexOf(" ")==(str.length-1))) {
str = str.substring(0, str.length-1);
}
return str;
}
//
//身份證字號檢查器 - 累加檢查碼
function getPID_SUM(sPID) {
var iChkNum = 0;

// 第 1 碼
iChkNum = ALP_STR.indexOf(sPID.substr(0,1)) + 10;
iChkNum = Math.floor(iChkNum/10) + (iChkNum%10*9);

// 第 2 - 9 碼
for(var i=1; i<sPID.length-1; i++) {
iChkNum += sPID.substr(i,1) * (9-i);
}

// 第 10 碼
iChkNum += sPID.substr(9,1)*1;

return iChkNum;
}

//
// 身分證字號檢查器 - 檢查合法字元
function chkPID_CHAR(sPID) {
var sMsg = "";
//sPID = trim(sPID.toUpperCase());
var iPIDLen = String(sPID).length;

var sChk = ALP_STR + NUM_STR;
for(i=0;i<iPIDLen;i++) {
if (sChk.indexOf(sPID.substr(i,1)) < 0) {
sMsg = "這個身分證字號含有不正確的字元!";
break;
}
}

if (sMsg.length == 0) {
if (ALP_STR.indexOf(sPID.substr(0,1)) < 0) {
sMsg = "身分證字號第 1 碼應為英文字母(A~Z)。";
} else if ((sPID.substr(1,1) != "1") && (sPID.substr(1,1) != "2")) {
sMsg = "身分證字號第 2 碼應為數字(1~2)。";
} else {
for(var i=2; i<iPIDLen; i++) {
if (NUM_STR.indexOf(sPID.substr(i, 1)) < 0) {
sMsg = "第 " + (i+1) + " 碼應為數字(0~9)。";
break;
}
}
}
}

if (sMsg.length != 0) {
document.getElementById("userIdMessage").innerHTML = sMsg;
return false;
} else {
document.getElementById("userIdMessage").innerHTML = "正確";
return true;
}
}

//
// 身分證字號檢查器
function checkID(sPID) {
var sMsg = "";

if (sPID == '') {
sMsg = "請輸入身分證字號";
} else if (sPID.length != 10) {
sMsg = "長度應為 10 !";
} else {
sPID = trim(sPID.toUpperCase());
if (!chkPID_CHAR(sPID)) return;

var iChkNum = getPID_SUM(sPID);

if (iChkNum % 10 != 0) {
var iLastNum = sPID.substr(9, 1) * 1;
for (i=0; i<10; i++) {
var xRightAlpNum = iChkNum - iLastNum + i;
if ((xRightAlpNum % 10) ==0) {
sMsg = "身分證字號不正確!";
break;
}
}
}

function sendRequest(); //引入Ajax
}

if (sMsg.length != 0) {
document.getElementById("userIdMessage").innerHTML = sMsg;
} else {
document.getElementById("userIdMessage").innerHTML = "正確";
}
}

</script>
<form name="user_data" method="POST" action="../member/personal_data.php" onSubmit="return checkForm();">

<table width="650" border="0" height="303" align="center" bgcolor="#EEEEEE" cellspacing="0" cellpadding="10px">
<tr align="left">
<td bgcolor="#FFFFCC" class="style37" style="font-weight: bold">
身分證號碼
</td>
<td height="18" bgcolor="#FFFFCC">
<input name="user_id" type="text" id="user_id" size='10' value='A123456789' maxlength='10' onblur="checkID(document.user_data.user_id.value)" />
<span id="userIdMessage" class="style29"></span>
</td>
</tr>

</table>
<p/>
<div align="center">
<input type="reset" name="reset" value="清除重填" />
<input type="button" name="Submit" disabled="disabled" value="下一頁" />
</div>
</form>

</body>
</html>


原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4454

評論

Sign In or Register to comment.