jquery表單驗(yàn)證使用插件formValidator
更新時(shí)間:2012年11月10日 10:56:07 作者:
jquery表單驗(yàn)證使用插件formValidator,可供有需求的朋友參考
1.首先在項(xiàng)目中添加必備js與css
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <!--jquery必須庫(kù)-->
<script src="formValidator1/formValidator-4.0.1.min.js" type="text/javascript"></script> <!--表單驗(yàn)證必須庫(kù)-->
<script src="formValidator1/formValidatorRegex.js" type="text/javascript"></script> <!--表單驗(yàn)證擴(kuò)展庫(kù)-->
<link href="formValidator1/style/validator.css" rel="stylesheet" type="text/css" /><!--表單驗(yàn)證樣式表-->
3.<body>中要驗(yàn)證的標(biāo)簽(做一些常用的演示)
<table border="0px" style="font-size:12px">
<tr>
<td colpan="3"><input type="submit" name="button" id="button" value="提交" /></td>
</tr>
<tr>
<td align="right">身份證(正則表達(dá)式庫(kù)):</td>
<td><input name="sfz" type="text" id="sfz" /></td>
<td><div id="sfzTip" style="width:300px"></div></td> <!--必須注意這里提示驗(yàn)證信息中的div的id值,跟要驗(yàn)證控件中的id值多了一個(gè)Tip,下面的都是這樣。必須多的是Tip,也必須要多。-->
</tr>
<tr>
<td align="right">身份證(外部函數(shù)):</td>
<td><input type="text" id="sfz1" style="width:120px" /></td>
<td><div id="sfz1Tip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">整數(shù):</td>
<td><input type="text" id="zs" style="width:120px" /></td>
<td><div id="zsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">正整數(shù):</td>
<td><input type="text" id="zzs" style="width:120px" /></td>
<td><div id="zzsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">負(fù)整數(shù):</td>
<td><input type="text" id="fzs" style="width:120px" /></td>
<td><div id="fzsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">數(shù)字:</td>
<td><input type="text" id="sz" style="width:120px" /></td>
<td><div id="szTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">正數(shù)(正整數(shù) + 0):</td>
<td><input type="text" id="zs1" style="width:120px" /></td>
<td><div id="zs1Tip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">負(fù)數(shù)(負(fù)整數(shù) + 0):</td>
<td><input type="text" id="fs" style="width:120px" /></td>
<td><div id="fsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">浮點(diǎn)數(shù):</td>
<td><input type="text" id="fds" style="width:120px" /></td>
<td><div id="fdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">正浮點(diǎn)數(shù):</td>
<td><input type="text" id="zfds" style="width:120px" /></td>
<td><div id="zfdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">負(fù)浮點(diǎn)數(shù):</td>
<td><input type="text" id="ffds" style="width:120px" /></td>
<td><div id="ffdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0):</td>
<td><input type="text" id="fffds" style="width:120px" /></td>
<td><div id="fffdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0):</td>
<td><input type="text" id="fzfds" style="width:120px" /></td>
<td><div id="fzfdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">顏色:</td>
<td><input type="text" id="ys" style="width:120px" /></td>
<td><div id="ysTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">你的EMAIL:</td>
<td><input type="text" id="email" style="width:120px" /></td>
<td><div id="emailTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">手機(jī):</td>
<td><input type="text" id="sj" style="width:120px" /></td>
<td><div id="sjTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">郵編:</td>
<td><input type="text" id="yb" style="width:120px" /></td>
<td><div id="ybTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">非空:</td>
<td><input type="text" id="fk" style="width:120px" /></td>
<td><div id="fkTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">圖片:</td>
<td><input type="text" id="tp" style="width:120px" /></td>
<td><div id="tpTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">壓縮文件:</td>
<td><input type="text" id="rar" style="width:120px" /></td>
<td><div id="rarTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">ip4:</td>
<td><input type="text" id="ip4" style="width:120px" /></td>
<td><div id="ip4Tip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">QQ號(hào)碼:</td>
<td><input type="text" id="qq" style="width:120px" /></td>
<td><div id="qqTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">國(guó)內(nèi)電話:</td>
<td><input type="text" id="dh" style="width:120px" /></td>
<td><div id="dhTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">用戶名:</td>
<td><input type="text" id="yhm" style="width:120px" /></td>
<td><div id="yhmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">字母:</td>
<td><input type="text" id="zm" style="width:120px" /></td>
<td><div id="zmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">大寫(xiě)字母:</td>
<td><input type="text" id="dxzm" style="width:120px" /></td>
<td><div id="dxzmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">小寫(xiě)字母:</td>
<td><input type="text" id="xxzm" style="width:120px" /></td>
<td><div id="xxzmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">身份證:</td>
<td><input type="text" id="sfz" style="width:120px" /></td>
<td><div id="sfzTip" style="width:300px"></div></td>
</tr>
</table>
4.<script>中的代碼
<script type="text/javascript">
$(document).ready(function () {
$.formValidator.initConfig({ formID: "form1", onError: function () { alert("校驗(yàn)沒(méi)有通過(guò),具體錯(cuò)誤請(qǐng)看錯(cuò)誤提示") } });
$("#sfz").formValidator({ onShow: "請(qǐng)輸入15或18位的身份證", onfocus: "輸入15或18位的身份證", onCorrect: "輸入正確" }).regexValidator({ regExp: "idcard", dataType: "enum", onError: "你輸入的身份證格式不正確" }); ;
$("#sfz1").formValidator({ onShow: "請(qǐng)輸入15或18位的身份證", onfocus: "輸入15或18位的身份證", onCorrect: "輸入正確" }).functionValidator({ fun: isCardID });
$("#zs").formValidator({ onShow: "請(qǐng)輸入整數(shù)", onCorrect: "謝謝你的合作,你的整數(shù)正確" }).regexValidator({ regExp: "intege", dataType: "enum", onError: "整數(shù)格式不正確" });
$("#zzs").formValidator({ onShow: "請(qǐng)輸入正整數(shù)", onCorrect: "謝謝你的合作,你的正整數(shù)正確" }).regexValidator({ regExp: "intege1", dataType: "enum", onError: "正整數(shù)格式不正確" });
$("#fzs").formValidator({ onShow: "請(qǐng)輸入負(fù)整數(shù)", onCorrect: "謝謝你的合作,你的負(fù)整數(shù)正確" }).regexValidator({ regExp: "intege2", dataType: "enum", onError: "負(fù)整數(shù)格式不正確" });
$("#sz").formValidator({ onShow: "請(qǐng)輸入數(shù)字", onCorrect: "謝謝你的合作,你的數(shù)字正確" }).regexValidator({ regExp: "num", dataType: "enum", onError: "數(shù)字格式不正確" });
$("#zs1").formValidator({ onShow: "請(qǐng)輸入正數(shù)", onCorrect: "謝謝你的合作,你的正數(shù)正確" }).regexValidator({ regExp: "num1", dataType: "enum", onError: "正數(shù)格式不正確" });
$("#fs").formValidator({ onShow: "請(qǐng)輸入負(fù)數(shù)", onCorrect: "謝謝你的合作,你的負(fù)數(shù)正確" }).regexValidator({ regExp: "num2", dataType: "enum", onError: "負(fù)數(shù)格式不正確" });
$("#sj").formValidator({ onShow: "請(qǐng)輸入你的手機(jī)號(hào)碼", onfocus: "必須是13或15打頭哦", onCorrect: "謝謝你的合作,你的手機(jī)號(hào)碼正確" }).regexValidator({ regExp: "mobile", dataType: "enum", onError: "手機(jī)號(hào)碼格式不正確" });
//$("#").formValidator({onShow:"",onfocus:"請(qǐng)輸入",onCorrect:"謝謝你的合作,你的正確"}).regexValidator({regExp:"",dataType:"enum",onError:"格式不正確"});
$("#email").formValidator({ onShow: "請(qǐng)輸入你的email", onfocus: "請(qǐng)注意你輸入的email格式,例如:wzmaodong@126.com", onCorrect: "謝謝你的合作,你的email正確" }).regexValidator({ regExp: "email", dataType: "enum", onError: "email格式不正確" });
$("#fds").formValidator({ onShow: "請(qǐng)輸入浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal", dataType: "enum", onError: "浮點(diǎn)數(shù)格式不正確" });
$("#zfds").formValidator({ onShow: "請(qǐng)輸入正浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的正浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal1", dataType: "enum", onError: "正浮點(diǎn)數(shù)格式不正確" });
$("#ffds").formValidator({ onShow: "請(qǐng)輸入負(fù)浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的負(fù)浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal2", dataType: "enum", onError: "負(fù)浮點(diǎn)數(shù)格式不正確" });
$("#fffds").formValidator({ onShow: "請(qǐng)輸入非負(fù)浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的非負(fù)浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal4", dataType: "enum", onError: "非負(fù)浮點(diǎn)數(shù)格式不正確" });
$("#fzfds").formValidator({ onShow: "請(qǐng)輸入非正浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的非正浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal5", dataType: "enum", onError: "非正浮點(diǎn)數(shù)格式不正確" });
$("#ys").formValidator({ onShow: "請(qǐng)輸入16進(jìn)制顏色", onCorrect: "謝謝你的合作,你的16進(jìn)制顏色正確" }).regexValidator({ regExp: "color", dataType: "enum", onError: "16進(jìn)制顏色格式不正確" });
$("#yb").formValidator({ onShow: "請(qǐng)輸入郵編", onfocus: "6位數(shù)字組成的哦", onCorrect: "謝謝你的合作,你的郵編正確" }).regexValidator({ regExp: "zipcode", dataType: "enum", onError: "郵編格式不正確" });
$("#ip4").formValidator({ onShow: "請(qǐng)輸入ip4", onfocus: "例如:172.16.201.18", onCorrect: "謝謝你的合作,你的ip4正確" }).regexValidator({ regExp: "ip4", dataType: "enum", onError: "ip4格式不正確" });
$("#fk").formValidator({ onShow: "請(qǐng)輸入非空字符", onCorrect: "謝謝你的合作,你的非空字符正確" }).regexValidator({ regExp: "notempty", dataType: "enum", onError: "非空字符格式不正確" });
$("#tp").formValidator({ onShow: "請(qǐng)輸入圖片名", onCorrect: "謝謝你的合作,你的圖片名正確" }).regexValidator({ regExp: "picture", dataType: "enum", onError: "圖片名格式不正確" });
$("#rar").formValidator({ onShow: "請(qǐng)輸入壓縮文件名", onCorrect: "謝謝你的合作,你的壓縮文件名正確" }).regexValidator({ regExp: "rar", dataType: "enum", onError: "壓縮文件名格式不正確" });
$("#qq").formValidator({ onShow: "請(qǐng)輸入QQ號(hào)碼", onCorrect: "謝謝你的合作,你的QQ號(hào)碼正確" }).regexValidator({ regExp: "qq", dataType: "enum", onError: "QQ號(hào)碼格式不正確" });
$("#dh").formValidator({ onShow: "請(qǐng)輸入國(guó)內(nèi)電話", onfocus: "例如:0577-88888888或省略區(qū)號(hào)88888888", onCorrect: "謝謝你的合作,你的國(guó)內(nèi)電話正確" }).regexValidator({ regExp: "tel", dataType: "enum", onError: "國(guó)內(nèi)電話格式不正確" });
$("#yhm").formValidator({ onShow: "請(qǐng)輸入用戶名", onCorrect: "謝謝你的合作,你的用戶名正確" }).regexValidator({ regExp: "username", dataType: "enum", onError: "用戶名格式不正確" });
$("#zm").formValidator({ onShow: "請(qǐng)輸入字母", onCorrect: "謝謝你的合作,你的字母正確" }).regexValidator({ regExp: "letter", dataType: "enum", onError: "字母格式不正確" });
$("#dxzm").formValidator({ onShow: "請(qǐng)輸入大寫(xiě)字母", onCorrect: "謝謝你的合作,你的大寫(xiě)字母正確" }).regexValidator({ regExp: "letter_u", dataType: "enum", onError: "大寫(xiě)字母格式不正確" });
$("#xxzm").formValidator({ onShow: "請(qǐng)輸入小寫(xiě)字母", onCorrect: "謝謝你的合作,你的小寫(xiě)字母正確" }).regexValidator({ regExp: "letter_l", dataType: "enum", onError: "小寫(xiě)字母格式不正確" });
$("#sfz").formValidator({ onShow: "請(qǐng)輸入身份證", onCorrect: "謝謝你的合作,你的身份證正確" }).regexValidator({ regExp: "idcard", dataType: "enum", onError: "身份證格式不正確" });
});
</script>
5.效果圖:
6.添加修改說(shuō)明:
$.formValidator.reloadAutoTip(); //重新加載表單驗(yàn)證樣式
if(!$.formValidator.pageIsValid('1')) return false; //如果不通過(guò)則不提交
$(document).ready(function () {
$.formValidator.initConfig({ formID: "form1", onError: function () { alert("校驗(yàn)沒(méi)有通過(guò),具體錯(cuò)誤請(qǐng)看錯(cuò)誤提示") } });
$("#name").formValidator({ onShow: "請(qǐng)輸入姓名!", onFocus: "輸入一個(gè)到十個(gè)字符", onCorrect: "輸入正確,謝謝您的合作!" }).inputValidator({ min: 1, max: 20, onError: "輸入長(zhǎng)度,不對(duì)!" });
$("#timelong").formValidator({ onShow: "請(qǐng)輸入路演時(shí)長(zhǎng)!", onFocus: "輸入格式為正整數(shù)!", onCorrect: "輸入正確,謝謝您的合作!" }).regexValidator({ regExp:"intege1",dataType: "enum", onError: "您輸入的時(shí)長(zhǎng)格式不正確!" });
$("#address").formValidator({ onShow: "請(qǐng)輸入路演平臺(tái)名稱!", onFocus: "輸入一個(gè)到五十個(gè)字符", onCorrect: "輸入正確,謝謝您的合作!" }).inputValidator({ min: 1, max: 50, onError: "輸入長(zhǎng)度,不對(duì)!" });
$("#urladdress").formValidator({ onShow: "請(qǐng)輸入路演鏈接地址!", onFocus: "輸入格式:http://www.baidu.com", onCorrect: "輸入正確,謝謝您的合作!" }).regexValidator({ regExp:"^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$", onError: "輸入格式不正確!" });
$("#time").formValidator({ onCorrect: "格式正確" }).functionValidator({fun:isDateTime, onError: "輸入格式,不對(duì)!" });
$("#txtcontent").formValidator({ onShow: "請(qǐng)輸入專家簡(jiǎn)介!", onFocus: "輸入一個(gè)到一百個(gè)字符", onCorrect: "輸入正確,謝謝您的合作!" }).inputValidator({ min: 1, max: 100, onError: "輸入長(zhǎng)度,不對(duì)!" });
});
2.代碼中添加引用(必備引用)
復(fù)制代碼 代碼如下:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <!--jquery必須庫(kù)-->
<script src="formValidator1/formValidator-4.0.1.min.js" type="text/javascript"></script> <!--表單驗(yàn)證必須庫(kù)-->
<script src="formValidator1/formValidatorRegex.js" type="text/javascript"></script> <!--表單驗(yàn)證擴(kuò)展庫(kù)-->
<link href="formValidator1/style/validator.css" rel="stylesheet" type="text/css" /><!--表單驗(yàn)證樣式表-->
3.<body>中要驗(yàn)證的標(biāo)簽(做一些常用的演示)
復(fù)制代碼 代碼如下:
<table border="0px" style="font-size:12px">
<tr>
<td colpan="3"><input type="submit" name="button" id="button" value="提交" /></td>
</tr>
<tr>
<td align="right">身份證(正則表達(dá)式庫(kù)):</td>
<td><input name="sfz" type="text" id="sfz" /></td>
<td><div id="sfzTip" style="width:300px"></div></td> <!--必須注意這里提示驗(yàn)證信息中的div的id值,跟要驗(yàn)證控件中的id值多了一個(gè)Tip,下面的都是這樣。必須多的是Tip,也必須要多。-->
</tr>
<tr>
<td align="right">身份證(外部函數(shù)):</td>
<td><input type="text" id="sfz1" style="width:120px" /></td>
<td><div id="sfz1Tip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">整數(shù):</td>
<td><input type="text" id="zs" style="width:120px" /></td>
<td><div id="zsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">正整數(shù):</td>
<td><input type="text" id="zzs" style="width:120px" /></td>
<td><div id="zzsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">負(fù)整數(shù):</td>
<td><input type="text" id="fzs" style="width:120px" /></td>
<td><div id="fzsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">數(shù)字:</td>
<td><input type="text" id="sz" style="width:120px" /></td>
<td><div id="szTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">正數(shù)(正整數(shù) + 0):</td>
<td><input type="text" id="zs1" style="width:120px" /></td>
<td><div id="zs1Tip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">負(fù)數(shù)(負(fù)整數(shù) + 0):</td>
<td><input type="text" id="fs" style="width:120px" /></td>
<td><div id="fsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">浮點(diǎn)數(shù):</td>
<td><input type="text" id="fds" style="width:120px" /></td>
<td><div id="fdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">正浮點(diǎn)數(shù):</td>
<td><input type="text" id="zfds" style="width:120px" /></td>
<td><div id="zfdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">負(fù)浮點(diǎn)數(shù):</td>
<td><input type="text" id="ffds" style="width:120px" /></td>
<td><div id="ffdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0):</td>
<td><input type="text" id="fffds" style="width:120px" /></td>
<td><div id="fffdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0):</td>
<td><input type="text" id="fzfds" style="width:120px" /></td>
<td><div id="fzfdsTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">顏色:</td>
<td><input type="text" id="ys" style="width:120px" /></td>
<td><div id="ysTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">你的EMAIL:</td>
<td><input type="text" id="email" style="width:120px" /></td>
<td><div id="emailTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">手機(jī):</td>
<td><input type="text" id="sj" style="width:120px" /></td>
<td><div id="sjTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">郵編:</td>
<td><input type="text" id="yb" style="width:120px" /></td>
<td><div id="ybTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">非空:</td>
<td><input type="text" id="fk" style="width:120px" /></td>
<td><div id="fkTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">圖片:</td>
<td><input type="text" id="tp" style="width:120px" /></td>
<td><div id="tpTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">壓縮文件:</td>
<td><input type="text" id="rar" style="width:120px" /></td>
<td><div id="rarTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">ip4:</td>
<td><input type="text" id="ip4" style="width:120px" /></td>
<td><div id="ip4Tip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">QQ號(hào)碼:</td>
<td><input type="text" id="qq" style="width:120px" /></td>
<td><div id="qqTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">國(guó)內(nèi)電話:</td>
<td><input type="text" id="dh" style="width:120px" /></td>
<td><div id="dhTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">用戶名:</td>
<td><input type="text" id="yhm" style="width:120px" /></td>
<td><div id="yhmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">字母:</td>
<td><input type="text" id="zm" style="width:120px" /></td>
<td><div id="zmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">大寫(xiě)字母:</td>
<td><input type="text" id="dxzm" style="width:120px" /></td>
<td><div id="dxzmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">小寫(xiě)字母:</td>
<td><input type="text" id="xxzm" style="width:120px" /></td>
<td><div id="xxzmTip" style="width:300px"></div></td>
</tr>
<tr>
<td align="right">身份證:</td>
<td><input type="text" id="sfz" style="width:120px" /></td>
<td><div id="sfzTip" style="width:300px"></div></td>
</tr>
</table>
4.<script>中的代碼
復(fù)制代碼 代碼如下:
<script type="text/javascript">
$(document).ready(function () {
$.formValidator.initConfig({ formID: "form1", onError: function () { alert("校驗(yàn)沒(méi)有通過(guò),具體錯(cuò)誤請(qǐng)看錯(cuò)誤提示") } });
$("#sfz").formValidator({ onShow: "請(qǐng)輸入15或18位的身份證", onfocus: "輸入15或18位的身份證", onCorrect: "輸入正確" }).regexValidator({ regExp: "idcard", dataType: "enum", onError: "你輸入的身份證格式不正確" }); ;
$("#sfz1").formValidator({ onShow: "請(qǐng)輸入15或18位的身份證", onfocus: "輸入15或18位的身份證", onCorrect: "輸入正確" }).functionValidator({ fun: isCardID });
$("#zs").formValidator({ onShow: "請(qǐng)輸入整數(shù)", onCorrect: "謝謝你的合作,你的整數(shù)正確" }).regexValidator({ regExp: "intege", dataType: "enum", onError: "整數(shù)格式不正確" });
$("#zzs").formValidator({ onShow: "請(qǐng)輸入正整數(shù)", onCorrect: "謝謝你的合作,你的正整數(shù)正確" }).regexValidator({ regExp: "intege1", dataType: "enum", onError: "正整數(shù)格式不正確" });
$("#fzs").formValidator({ onShow: "請(qǐng)輸入負(fù)整數(shù)", onCorrect: "謝謝你的合作,你的負(fù)整數(shù)正確" }).regexValidator({ regExp: "intege2", dataType: "enum", onError: "負(fù)整數(shù)格式不正確" });
$("#sz").formValidator({ onShow: "請(qǐng)輸入數(shù)字", onCorrect: "謝謝你的合作,你的數(shù)字正確" }).regexValidator({ regExp: "num", dataType: "enum", onError: "數(shù)字格式不正確" });
$("#zs1").formValidator({ onShow: "請(qǐng)輸入正數(shù)", onCorrect: "謝謝你的合作,你的正數(shù)正確" }).regexValidator({ regExp: "num1", dataType: "enum", onError: "正數(shù)格式不正確" });
$("#fs").formValidator({ onShow: "請(qǐng)輸入負(fù)數(shù)", onCorrect: "謝謝你的合作,你的負(fù)數(shù)正確" }).regexValidator({ regExp: "num2", dataType: "enum", onError: "負(fù)數(shù)格式不正確" });
$("#sj").formValidator({ onShow: "請(qǐng)輸入你的手機(jī)號(hào)碼", onfocus: "必須是13或15打頭哦", onCorrect: "謝謝你的合作,你的手機(jī)號(hào)碼正確" }).regexValidator({ regExp: "mobile", dataType: "enum", onError: "手機(jī)號(hào)碼格式不正確" });
//$("#").formValidator({onShow:"",onfocus:"請(qǐng)輸入",onCorrect:"謝謝你的合作,你的正確"}).regexValidator({regExp:"",dataType:"enum",onError:"格式不正確"});
$("#email").formValidator({ onShow: "請(qǐng)輸入你的email", onfocus: "請(qǐng)注意你輸入的email格式,例如:wzmaodong@126.com", onCorrect: "謝謝你的合作,你的email正確" }).regexValidator({ regExp: "email", dataType: "enum", onError: "email格式不正確" });
$("#fds").formValidator({ onShow: "請(qǐng)輸入浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal", dataType: "enum", onError: "浮點(diǎn)數(shù)格式不正確" });
$("#zfds").formValidator({ onShow: "請(qǐng)輸入正浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的正浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal1", dataType: "enum", onError: "正浮點(diǎn)數(shù)格式不正確" });
$("#ffds").formValidator({ onShow: "請(qǐng)輸入負(fù)浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的負(fù)浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal2", dataType: "enum", onError: "負(fù)浮點(diǎn)數(shù)格式不正確" });
$("#fffds").formValidator({ onShow: "請(qǐng)輸入非負(fù)浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的非負(fù)浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal4", dataType: "enum", onError: "非負(fù)浮點(diǎn)數(shù)格式不正確" });
$("#fzfds").formValidator({ onShow: "請(qǐng)輸入非正浮點(diǎn)數(shù)", onCorrect: "謝謝你的合作,你的非正浮點(diǎn)數(shù)正確" }).regexValidator({ regExp: "decmal5", dataType: "enum", onError: "非正浮點(diǎn)數(shù)格式不正確" });
$("#ys").formValidator({ onShow: "請(qǐng)輸入16進(jìn)制顏色", onCorrect: "謝謝你的合作,你的16進(jìn)制顏色正確" }).regexValidator({ regExp: "color", dataType: "enum", onError: "16進(jìn)制顏色格式不正確" });
$("#yb").formValidator({ onShow: "請(qǐng)輸入郵編", onfocus: "6位數(shù)字組成的哦", onCorrect: "謝謝你的合作,你的郵編正確" }).regexValidator({ regExp: "zipcode", dataType: "enum", onError: "郵編格式不正確" });
$("#ip4").formValidator({ onShow: "請(qǐng)輸入ip4", onfocus: "例如:172.16.201.18", onCorrect: "謝謝你的合作,你的ip4正確" }).regexValidator({ regExp: "ip4", dataType: "enum", onError: "ip4格式不正確" });
$("#fk").formValidator({ onShow: "請(qǐng)輸入非空字符", onCorrect: "謝謝你的合作,你的非空字符正確" }).regexValidator({ regExp: "notempty", dataType: "enum", onError: "非空字符格式不正確" });
$("#tp").formValidator({ onShow: "請(qǐng)輸入圖片名", onCorrect: "謝謝你的合作,你的圖片名正確" }).regexValidator({ regExp: "picture", dataType: "enum", onError: "圖片名格式不正確" });
$("#rar").formValidator({ onShow: "請(qǐng)輸入壓縮文件名", onCorrect: "謝謝你的合作,你的壓縮文件名正確" }).regexValidator({ regExp: "rar", dataType: "enum", onError: "壓縮文件名格式不正確" });
$("#qq").formValidator({ onShow: "請(qǐng)輸入QQ號(hào)碼", onCorrect: "謝謝你的合作,你的QQ號(hào)碼正確" }).regexValidator({ regExp: "qq", dataType: "enum", onError: "QQ號(hào)碼格式不正確" });
$("#dh").formValidator({ onShow: "請(qǐng)輸入國(guó)內(nèi)電話", onfocus: "例如:0577-88888888或省略區(qū)號(hào)88888888", onCorrect: "謝謝你的合作,你的國(guó)內(nèi)電話正確" }).regexValidator({ regExp: "tel", dataType: "enum", onError: "國(guó)內(nèi)電話格式不正確" });
$("#yhm").formValidator({ onShow: "請(qǐng)輸入用戶名", onCorrect: "謝謝你的合作,你的用戶名正確" }).regexValidator({ regExp: "username", dataType: "enum", onError: "用戶名格式不正確" });
$("#zm").formValidator({ onShow: "請(qǐng)輸入字母", onCorrect: "謝謝你的合作,你的字母正確" }).regexValidator({ regExp: "letter", dataType: "enum", onError: "字母格式不正確" });
$("#dxzm").formValidator({ onShow: "請(qǐng)輸入大寫(xiě)字母", onCorrect: "謝謝你的合作,你的大寫(xiě)字母正確" }).regexValidator({ regExp: "letter_u", dataType: "enum", onError: "大寫(xiě)字母格式不正確" });
$("#xxzm").formValidator({ onShow: "請(qǐng)輸入小寫(xiě)字母", onCorrect: "謝謝你的合作,你的小寫(xiě)字母正確" }).regexValidator({ regExp: "letter_l", dataType: "enum", onError: "小寫(xiě)字母格式不正確" });
$("#sfz").formValidator({ onShow: "請(qǐng)輸入身份證", onCorrect: "謝謝你的合作,你的身份證正確" }).regexValidator({ regExp: "idcard", dataType: "enum", onError: "身份證格式不正確" });
});
</script>
5.效果圖:
6.<script>中函數(shù)參數(shù)說(shuō)明:
formValidator: | 用來(lái)做初始化的類(lèi)型,必須先執(zhí)行。("√"為showalert可用參數(shù)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inputValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
compareValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regexValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ajaxValidator: | 幾乎所有的屬性跟$.ajax()的屬性一樣,請(qǐng)參考$.ajax()函數(shù)的幫助 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
functionValidator | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
公共函數(shù): | 主要是設(shè)置全局參數(shù)和判斷是否通過(guò)校驗(yàn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
formValidator: | 用來(lái)做初始化的類(lèi)型,必須先執(zhí)行。("√"為showalert可用參數(shù)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inputValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
compareValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regexValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ajaxValidator: | 幾乎所有的屬性跟$.ajax()的屬性一樣,請(qǐng)參考$.ajax()函數(shù)的幫助 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
functionValidator | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
公共函數(shù): | 主要是設(shè)置全局參數(shù)和判斷是否通過(guò)校驗(yàn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
formValidator: | 用來(lái)做初始化的類(lèi)型,必須先執(zhí)行。("√"為showalert可用參數(shù)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
inputValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
compareValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
regexValidator: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ajaxValidator: | 幾乎所有的屬性跟$.ajax()的屬性一樣,請(qǐng)參考$.ajax()函數(shù)的幫助 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
functionValidator | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
公共函數(shù): | 主要是設(shè)置全局參數(shù)和判斷是否通過(guò)校驗(yàn) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
6.添加修改說(shuō)明:
復(fù)制代碼 代碼如下:
$.formValidator.reloadAutoTip(); //重新加載表單驗(yàn)證樣式
if(!$.formValidator.pageIsValid('1')) return false; //如果不通過(guò)則不提交
復(fù)制代碼 代碼如下:
$(document).ready(function () {
$.formValidator.initConfig({ formID: "form1", onError: function () { alert("校驗(yàn)沒(méi)有通過(guò),具體錯(cuò)誤請(qǐng)看錯(cuò)誤提示") } });
$("#name").formValidator({ onShow: "請(qǐng)輸入姓名!", onFocus: "輸入一個(gè)到十個(gè)字符", onCorrect: "輸入正確,謝謝您的合作!" }).inputValidator({ min: 1, max: 20, onError: "輸入長(zhǎng)度,不對(duì)!" });
$("#timelong").formValidator({ onShow: "請(qǐng)輸入路演時(shí)長(zhǎng)!", onFocus: "輸入格式為正整數(shù)!", onCorrect: "輸入正確,謝謝您的合作!" }).regexValidator({ regExp:"intege1",dataType: "enum", onError: "您輸入的時(shí)長(zhǎng)格式不正確!" });
$("#address").formValidator({ onShow: "請(qǐng)輸入路演平臺(tái)名稱!", onFocus: "輸入一個(gè)到五十個(gè)字符", onCorrect: "輸入正確,謝謝您的合作!" }).inputValidator({ min: 1, max: 50, onError: "輸入長(zhǎng)度,不對(duì)!" });
$("#urladdress").formValidator({ onShow: "請(qǐng)輸入路演鏈接地址!", onFocus: "輸入格式:http://www.baidu.com", onCorrect: "輸入正確,謝謝您的合作!" }).regexValidator({ regExp:"^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$", onError: "輸入格式不正確!" });
$("#time").formValidator({ onCorrect: "格式正確" }).functionValidator({fun:isDateTime, onError: "輸入格式,不對(duì)!" });
$("#txtcontent").formValidator({ onShow: "請(qǐng)輸入專家簡(jiǎn)介!", onFocus: "輸入一個(gè)到一百個(gè)字符", onCorrect: "輸入正確,謝謝您的合作!" }).inputValidator({ min: 1, max: 100, onError: "輸入長(zhǎng)度,不對(duì)!" });
});
您可能感興趣的文章:
- jquery validate.js表單驗(yàn)證的基本用法入門(mén)
- 基于Bootstrap+jQuery.validate實(shí)現(xiàn)Form表單驗(yàn)證
- Jquery練習(xí)之表單驗(yàn)證實(shí)現(xiàn)代碼
- 使用 jQuery 實(shí)現(xiàn)表單驗(yàn)證功能
- 基于Jquery實(shí)現(xiàn)表單驗(yàn)證
- jQuery 表單驗(yàn)證插件formValidation實(shí)現(xiàn)個(gè)性化錯(cuò)誤提示
- jquery實(shí)現(xiàn)表單驗(yàn)證并阻止非法提交
- jQuery實(shí)現(xiàn)用戶注冊(cè)的表單驗(yàn)證示例
- Jquery插件easyUi表單驗(yàn)證提交(示例代碼)
- jQuery實(shí)現(xiàn)表單驗(yàn)證功能
相關(guān)文章
更換select下拉菜單背景樣式的實(shí)現(xiàn)代碼
更換select下拉菜單背景樣式的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12input、button的不同type值在ajax提交表單時(shí)導(dǎo)致的陷阱
最近在公司的項(xiàng)目中嘗試著利用YUI來(lái)實(shí)現(xiàn)各種效果。2009-02-02