為了避免無效資料的另一種方法是在使用者錄入資料時 對無效輸入進行屏蔽, 例如在輸入銀行卡號時, 要求使用者必須輸入數字, 當使用者輸入非數字字元是,給出提示。
下面給出代碼:
[html] <!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=utf-8" />
<title>屏蔽 非數字字元的輸入</title>
<style>
body{ font:10px; }
span{ color:red; }
</style>
<script>
function is_number(e){
//IE 中 Event 對象使用 keyCode 獲得鍵入字元的 Unicode 編碼
//DOM 中 Event 對象 使用 charCode 獲得鍵入字元的 Unicode 編碼
var char_code = e.charCode ? e.charCode : e.keyCode;
//Unicode 編碼中, 0~9 的編碼的十進位 是 48~57 之間 , 0為 48, 9 為57
if(char_code<48 || char_code >57){
alert("只允許輸入數字");
return false;
}
else{
return true;
}
}
</script>
</head>
<body>
<form name="user_info" method="post">
<table width="400" height="1" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="right">使用者名稱:</td>
<td align="left"><input type="text" name="user_name" size="15" /><span>*</span></td>
</tr>
<tr>
<td align="right">密碼:</td>
<td align="left"><input type="password" name="user_pwd" size="15" /><span>*</span></td>
</tr>
<tr>
<td align="right">銀行帳號:</td>
<td align="left"><input type="text" name="user_account_no" size="15" onkeypress="return is_number(event)" /><span>*</span></td>
</tr>
<tr>
<td align="right">E-mail:</td>
<td align="left"><input type="text" name="user_email" size="15" /><span>*</span></td>
</tr>
<tr>
<td align="right">簡歷:</td>
<td align="left"><textarea name="user_intro" cols="16" rows="5" ></textarea><span>*</span></td>
</tr>
<tr>
<td align="right"></td>
<td align="left"><input type="submit" value="提交" onclick="return check_form()" /></td>
</tr>
</table>
</form>
</body>
</html>
<!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=utf-8" />
<title>屏蔽 非數字字元的輸入</title>
<style>
body{ font:10px; }
span{ color:red; }
</style>
<script>
function is_number(e){
//IE 中 Event 對象使用 keyCode 獲得鍵入字元的 Unicode 編碼
//DOM 中 Event 對象 使用 charCode 獲得鍵入字元的 Unicode 編碼
var char_code = e.charCode ? e.charCode : e.keyCode;
//Unicode 編碼中, 0~9 的編碼的十進位 是 48~57 之間 , 0為 48, 9 為57
if(char_code<48 || char_code >57){
alert("只允許輸入數字");
return false;
}
else{
return true;
}
}
</script>
</head>
<body>
<form name="user_info" method="post">
<table width="400" height="1" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="right">使用者名稱:</td>
<td align="left"><input type="text" name="user_name" size="15" /><span>*</span></td>
</tr>
<tr>
<td align="right">密碼:</td>
<td align="left"><input type="password" name="user_pwd" size="15" /><span>*</span></td>
</tr>
<tr>
<td align="right">銀行帳號:</td>
<td align="left"><input type="text" name="user_account_no" size="15" onkeypress="return is_number(event)" /><span>*</span></td>
</tr>
<tr>
<td align="right">E-mail:</td>
<td align="left"><input type="text" name="user_email" size="15" /><span>*</span></td>
</tr>
<tr>
<td align="right">簡歷:</td>
<td align="left"><textarea name="user_intro" cols="16" rows="5" ></textarea><span>*</span></td>
</tr>
<tr>
<td align="right"></td>
<td align="left"><input type="submit" value="提交" onclick="return check_form()" /></td>
</tr>
</table>
</form>
</body>
</html>
以上代碼中 , is_number() 函數 用於屏蔽非數字字元的輸入。 函數中, 通過Event 對象 的屬性 得到按下 鍵 的 Unicode 編碼, 對於 IE瀏覽器 採用 keyCode 屬性, 而對於 DOM 瀏覽器採用 charCode 屬性。 在 Unicode 編碼中 , 0~9 的編碼是 48~57(十進位)之間的連續值, 0 的編碼是 48 , 9的編碼是 57, 當 按下鍵的編碼 超出 48~57 這個有效範圍時, 變為非數字字元。
說明:
IE瀏覽器 沒有 charCode 屬性 , keyCode 屬性 在 keypress 事件中 得到 與 DOM 中 charCode 屬性相同的值, 及按下 鍵 的 Unicode 編碼。
is_number()函數 被賦予銀行帳號 文字框的onkeypress 事件 控制代碼(按下鍵後放開時觸發),當使用者鍵入字元時, 對鍵入的字元進行有效性的檢測。 如果檢測到鍵入的字元為非數字字元, 則返回 false。 keypress 事件 的預設 操作 是在 按鍵放開後 鍵入字元, 當 onkeypress 事件控制代碼得到 false 值 時, 便阻止預設操作即阻止字元的鍵入。
注意:
在 將 is_number() 函數 賦予 onkeypress 事件控制代碼時, 注意使用 return 關鍵字, 這樣才能阻止 非數字字元的鍵入。
Over!!!
摘自 SongYanJun2011