PCDVD數位科技討論區

PCDVD數位科技討論區 (https://www.pcdvd.com.tw/index.php)
-   七嘴八舌異言堂 (https://www.pcdvd.com.tw/forumdisplay.php?f=12)
-   -   網頁 Javascript 驗證身分證字號欄位問題 (https://www.pcdvd.com.tw/showthread.php?t=681661)

anos 2006-12-20 12:06 AM

網頁 Javascript 驗證身分證字號欄位問題
 
最近小弟寫網頁遇到兩個問題寫不出來 !
想煩請高人指點一下 !
就是身分證字號的欄位應為10 個字,第一個字為英文字母,第二個字為1 或2
剩這功能小弟時在研究好久...

下面是那張網頁的原始內容 ! 請大大們幫小弟解答嚕...

代碼:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<SCRIPT LANGUAGE="JavaScript"> 
<!--
function submitData()
{
  	var index=0;
	var text1="";
	//驗證姓名欄
    text2=document.myForm.username.value;
	if( text2.length <= 1 )  //姓名至少需2個字
	{
        alert('您尚未輸入姓名!姓名至少需2個字');
        document.myForm.username.focus();
        return false;
    }
   //驗證電子郵件信箱位址
    text1 = document.myForm.eMail.value;
    index = text1.indexOf('@');
    if (index == -1)
	{
		alert('您尚未輸入電子郵件信箱位址或信箱位址不正確!');
		document.myForm.eMail.focus;
        return false;
    }
	//驗證身份證號碼
	if( document.myForm.id.value == "" )
	{
        alert('身分證號碼欄不能為空白!');
        document.myForm.id.focus();
        return false;
    }
    return true;
} 
-->
</SCRIPT>
<title>習題11-javaScript表單驗證</title>
</head>

<body>
<h1 style='text-align:center; font-family: "標楷體", "Times New Roman"; color:#FF0000 '>
會員資料登錄</h1>
<hr>
<p style='text-align:left; color:#0000FF'><pre>

<form name="myForm" action="mailto:[email protected]?subject=exercise11" onSubmit="return submitData()" method="post" enctype="text/plain" >
<ul type=circle>
<li>會員姓名  : <input type="text" size="20" name="username">
<li>Email    :<input type="text" size="26" name="eMail">
<li>身分證號碼:<input type="text" size="20" name="id">
<li><input type="submit" name="submit1" value="確  定">
</form></ul></pre></p><hr></body></html>

kec 2006-12-20 06:01 AM

用Regular Expression來驗證會比較方便。

crazyjack 2006-12-20 08:58 AM

長度用length...取第幾個字元用charAt.....google一下就有嚕!

AdaHsu 2006-12-20 10:44 AM

其實,連 email 檢驗也可以從 google 中找到一大票!雖然仍然不能確保是否存在該 email ,但可以保證 email 的格式是正確的。

Chief_WU 2006-12-20 11:34 AM


代碼:
function CheckID(){
    ID=document.forms['表格名稱'].欄位.value.toLowerCase();
    pattern=/^[a-z][12][0-9]{8}$/ ;

    if(pattern.test(ID)){
        h="abcdefghjklmnpqrstuvxywzio" ;
        x=10+h.indexOf(ID.substring(0,1));
        chksum=(x-(x%10))/10+(x%10)*9;
        
        for(i=1;i<9;i++){
            chksum+=ID.substring(i,i+1)*(9-i);
        } 
    
        chksum=(10-chksum%10)%10 ;
    
        if(chksum==ID.substring(9,10)){
            return true;
        }
    }
    return false;
} 



所有的時間均為GMT +8。 現在的時間是11:39 PM.

vBulletin Version 3.0.1
powered_by_vbulletin 2025。