表單驗(yàn)證方式的通用化
此文是“Web頁面表單域驗(yàn)證方式的改進(jìn)”的續(xù)篇。在上一篇“Web頁面表單域驗(yàn)證方式的改進(jìn)“中,我們通過把驗(yàn)證法則(正則表達(dá)式和是否必填字段)寫在表單域中,將驗(yàn)證過程和驗(yàn)證法則成功的分離,從而減少了重復(fù)代碼,使驗(yàn)證變得簡(jiǎn)單易行,在實(shí)際使用中,我們可以把驗(yàn)證過程放在一個(gè)JS文件中,對(duì)于全輸入驗(yàn)證界面,在頁面的表單驗(yàn)證部分只需要調(diào)用其中的checkForm函數(shù)就能進(jìn)行有效驗(yàn)證,頁面上不再需要書寫重復(fù)性高的JS驗(yàn)證代碼;對(duì)于復(fù)雜的表單,比如其中含有復(fù)選框或是需要兩個(gè)文本框比較的地方,這種方法也可讓你不寫通用驗(yàn)證代碼而把主要精力放在特殊的驗(yàn)證上。這些能減輕不少工作量,讓繁瑣的工作變得輕松愉快起來。
下面我們看一看這種用法的實(shí)際使用。
首先,我們可以把驗(yàn)證過程放在一個(gè)JS文件中,具體代碼如下:
formchecker.js




















































下面想驗(yàn)證一個(gè)用戶登錄頁面,它的頁面部分代碼如下:
















































































請(qǐng)注意其中沒有任何頁面驗(yàn)證的JS代碼,只有在表單驗(yàn)證的地方調(diào)用formchecker.js中的函數(shù)。
不需要寫JS代碼,只需要引入formchecker.js,就實(shí)現(xiàn)了表單的驗(yàn)證,下面是驗(yàn)證效果之一:

對(duì)于復(fù)雜一些的頁面,在formchecker.js的幫助下也能有效減少驗(yàn)證代碼量,你只要書寫一些特殊的通過validchar不能驗(yàn)證的代碼即可,比如說如下注冊(cè)頁面:

你只要書寫兩次密碼比較的JS代碼,其它的還是可以讓checkForm函數(shù)幫你完成。具體代碼如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>"我的事務(wù)備忘錄"用戶注冊(cè)頁面</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="web/js/ajax.js" type="text/javascript"></script>
<script src="web/js/formchecker.js" type="text/javascript"></script>
<link rel="stylesheet" rev="stylesheet" href="web/css/style.css"
type="text/css" />
</head>
<body>
<div>
<%
String msg = (String) request.getAttribute("Msg");
if (msg != null) {
out.print(msg);
}
%>
<!-- 外層表格,比內(nèi)表格寬,以在左右留出一定空當(dāng) -->
<table style="border-right: 1px solid; border-top: 1px solid; border-left: 1px solid; border-bottom: 1px solid; boder_top: 0px" bordercolor="#236d78" cellspacing="0" cellpadding="0" width="530" align="center" border="0">
<tr bgcolor="#eaecf5" height="25">
<td colspan=3> <font face=webdings color=#ff8c00>8</font><b> 注冊(cè)個(gè)人用戶</b></td>
</tr>
<tr bgcolor=#236d78 height=1><td></td></tr>
<tr bgcolor=#eaecf5 >
<td bgcolor=#ffffff>
<!-- 內(nèi)置表格,居中,比外表格窄, -->
<form action="UserRegister" onsubmit="return check(this);">
<table width=460 align=center border=0>
<tbody>
<tr>
<td width=70>用戶名:</td>
<td>
<input type="text"
name="userName"
validChar="[\u4E00-\u9FA5]{2,3}"
isRequired="true"
onfocus="this.style.backgroundColor='#e6e6e6'"
onblur="this.style.backgroundColor='#ffffff'"/>
<font color=red> (必填)</font>
<span id="userNameMsg" class="feedbackHide">用戶名必須輸入兩到三位漢字</span>
</td>
</tr>
<tr>
<td width=70>密碼:</td>
<td>
<input type="text"
name="userPswd"
validChar="\w+"
isRequired="true"
onfocus="this.style.backgroundColor='#e6e6e6'"
onblur="this.style.backgroundColor='#ffffff'"/>
<font color=red> (必填)</font>
<span id="userPswdMsg" class="feedbackHide">密碼必須輸入英語或數(shù)字</span>
</td>
</tr>
<tr>
<td width=70>再次輸入密碼:</td>
<td>
<input type="text"
name="userPswd2"
validChar="\w+"
isRequired="true"
onfocus="this.style.backgroundColor='#e6e6e6'"
onblur="this.style.backgroundColor='#ffffff'"/>
<font color=red> (必填)</font>
<span id="userPswd2Msg" class="feedbackHide">密碼必須輸入英語或數(shù)字</span>
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="注冊(cè)"/></td>
</tr>
</tbody>
</table>
</form>
<!-- 內(nèi)置表格結(jié)束-->
</td>
</tr>
</table>
<!-- 外層表格結(jié)束 -->
<div>
</body>
</html>
<script LANGUAGE="JavaScript">
<!--
function check(vform){
// 先進(jìn)行文本框基本驗(yàn)證
if(checkForm(vform)==false){
return false;
}
// 取得密碼
var userPswd=$("userPswd").value;
// 取得第二次密碼
var userPswd2=$("userPswd2").value;
// 檢查兩次密碼是否對(duì)應(yīng)
if(userPswd2!=userPswd){
alert("兩次輸入的密碼不相同");
$("userPswd2").focus();
return false;
}
return true;
}
//-->
</script>
注意看上面的js代碼比常規(guī)方案減少了多少。
如果頁面上有其它控件如復(fù)選框,列表框等也可照此辦理,把文本框的通用驗(yàn)證交給formchecker.js的checkForm和checkTextBox進(jìn)行,在頁面的js代碼中只寫特殊的處理,這樣能減輕不少工作量,讓繁瑣的工作變得輕松愉快起來。
posted on 2008-04-07 21:41 sitinspring 閱讀(2342) 評(píng)論(7) 編輯 收藏 所屬分類: Web開發(fā) 、JavaScript