jquery.validate使用時(shí)遇到的問題
問題一:
<script src="../js/jquery.js"></script> <script src="../js/jquery.validate.js"></script> <script> $().ready(function() { $("#registerForm").validate(); }); </script> <form id="registerForm" method="get" action=""> <fieldset> <p> <label for="cusername">用戶名</label> <input id="cusername" name="username" type="text" data-rule-required="true" data-rule-rangelength="[2,10]" data-msg-required="用戶名不能為空" data-msg-rangelength="用戶名長度必須是2到10個(gè)字符"> </p> <p> <label for="cpassword">密碼</label> <input id="cpassword" name="password" type="password" data-rule-required="true" data-rule-minlength="6" data-msg-required="密碼不能為空" data-msg-minlength="至少設(shè)置6位密碼"> </p> <p> <label for="cconfirmpassword">確認(rèn)密碼</label> <input id="cconfirmpassword" name="confirmpassword" type="password" data-rule-equalTo="#cpassword" data-msg-equalTo="兩次密碼不一致"> </p> <p> <label for="cemail">郵箱</label> <input id="cemail" name="email" data-rule-required="true" data-rule-email="true" data-msg-required="郵箱不能為空" data-msg-email="郵箱的格式不正確"> </input> </p> <p> <label for="chasreferee">有推薦人請(qǐng)勾選</label> <input type="checkbox" id="chasreferee" name="hasreferee"> </p> <p> <label for="creferee">推薦人</label> <input id="creferee" name="referee" data-rule-required="#chasreferee:checked" data-msg-required="推薦人不能為空"> </input> </p> <p> <input type="submit" value="提交"> </p> </fieldset> </form>
看了之前的別人寫的文章,貌似是依賴jquery.metadata.js這個(gè)庫,然后寫的時(shí)候以 class=”required email” 這樣的形式來寫,這樣寫起來好像有些亂,class本身是呈現(xiàn)樣式的,現(xiàn)在被附上各種校驗(yàn)的規(guī)則,看上去似乎有些亂,不過好在新版本中,又有了新的寫法,不依賴上面的js庫,以 data-rule-驗(yàn)證規(guī)則、data-msg-提示信息 這樣的格式來重新定義,更簡(jiǎn)單,更直觀,更強(qiáng)大了。上面的測(cè)試通過
我的版本的jquery.validate1.13.js
然后這樣的寫法,控件中的messages不會(huì)生效,還會(huì)報(bào)錯(cuò):Cannot read property 'call' of undefined 園子里面很多jquery.validate文章提到可以使用,我看是版本過時(shí)了,反正我沒有試驗(yàn)出來。還有就是將驗(yàn)證卸載class里面我也是醉了。下面的測(cè)試錯(cuò)誤!
<!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>無標(biāo)題文檔</title> <script type="text/javascript" src="jquery.min.js"></script> <!--<script type="text/javascript" src="jquery.validate.js"></script>--> <script type="text/javascript" src="jquery.validate1.13.js"></script> <script type="text/javascript" src="jquery.validate.message_cn.js"></script> <script type="text/javascript" src="jquery.metadata.js"></script> <script type="text/javascript"> $(function(){ $.metadata.setType("attr", "validate"); $("#signupForm").validate(); //$("#signupForm").validate({ meta: "validate" }); //$("#commentForm").validate(); }) </script> </head> <body> <form id="signupForm" method="get" action=""> <p> <input id="email" name="email" validate="{required:true, email:true, messages:{required:'輸入email地址', email:'你輸入的不是有效的郵件地址'}}" /> </p> <p> <input class="submit" type="submit" value="Submit"/> </p> </form> </body> </html>
問題二:jQuery_validate配置后無論怎樣都看不到提示信息。
原因:submit()了二次。
例子:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>jquery test</title> <script src="js/jquery.js"></script> <script src="js/jquery.validate.js"></script> <script src="js/jquery.metadata.js"></script> <script src="js/messages_zh.js"></script> <script> $(document).ready(function() { $("#commentForm").validate({ //debug:true }); }); </script> <script type="text/javascript"> function register(){ document.forms[0].action = 'register/addUser.action'; //document.forms[0].submit(); } </script> </head> <body > <form id="commentForm" method="post"> <table style ="width:100%"> <tr> <td>user name:</td> <td><input type="text" name="username" id="username" maxlength="10" class="{required:true,minlength:6,maxlength:12}" /></td> </tr> <tr> <td>password:</td> <td><input type="password" name="password" id="password" maxlength="15" class="required"/></td> </tr> <tr> <td></td> <td><input type="submit" value="Register" onclick="register();"></td> </tr> </table> </form> </body> </html>
jQuery驗(yàn)證后有個(gè)提交,自己在register()中又提交了一次,將【document.forms[0].submit();】注釋掉后問題解決。
- jquery.validate 自定義驗(yàn)證方法及validate相關(guān)參數(shù)
- jQuery.Validate驗(yàn)證庫的使用介紹
- 基于Bootstrap+jQuery.validate實(shí)現(xiàn)Form表單驗(yàn)證
- jquery.validate提示錯(cuò)誤信息位置方法
- jQuery.validate 常用方法及需要注意的問題
- jquery.validate使用詳解
- jquery.validate的使用說明介紹
- 利用jQuery.Validate異步驗(yàn)證用戶名是否存在(推薦)
- 功能強(qiáng)大的jquery.validate表單驗(yàn)證插件
- jquery.validate自定義驗(yàn)證用法實(shí)例分析【成功提示與擇要提示】
相關(guān)文章
基于jQuery實(shí)現(xiàn)的無刷新表格分頁實(shí)例
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)的無刷新表格分頁方法,結(jié)合完整實(shí)例形式較為詳細(xì)的分析了jQuery實(shí)現(xiàn)無刷新表格分頁的具體步驟與相關(guān)實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-02-02jQuery動(dòng)態(tài)效果顯示人物結(jié)構(gòu)關(guān)系圖的方法
這篇文章主要介紹了jQuery動(dòng)態(tài)效果顯示人物結(jié)構(gòu)關(guān)系圖的方法,涉及jQuery操作json結(jié)構(gòu)數(shù)據(jù)及鼠標(biāo)事件的技巧,需要的朋友可以參考下2015-05-05jQuery實(shí)現(xiàn)自動(dòng)調(diào)整字體大小的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)自動(dòng)調(diào)整字體大小的方法,涉及jQuery針對(duì)頁面屬性與樣式動(dòng)態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06struts2+jquery+json實(shí)現(xiàn)異步加載數(shù)據(jù)(自寫)
異步加載數(shù)據(jù)利用struts2+jquery+json實(shí)現(xiàn),具體代碼如下,感興趣的各位可以參考下哈,希望對(duì)大家有所幫助2013-06-06jQuery獲取Select選擇的Text和Value(詳細(xì)匯總)
Select選擇的Text和Value在使用中可能都需要獲取到它們的值,以至搜集用戶的需求,本文整理了一些常用而實(shí)用的操作技巧,感興趣的朋友可以了解下,就當(dāng)鞏固自己的知識(shí)了,希望本文對(duì)你有所幫助2013-01-01jQuery pjax 應(yīng)用簡(jiǎn)單示例
此篇文章給大家分享了jQuery pjax 應(yīng)用的一些簡(jiǎn)單示例,希望可以幫助到大家2018-09-09jQuery實(shí)現(xiàn)的兼容性浮動(dòng)層示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的兼容性浮動(dòng)層,可兼容IE、火狐等主流瀏覽器,涉及jQuery針對(duì)頁面元素的運(yùn)算與屬性的動(dòng)態(tài)設(shè)置相關(guān)技巧,需要的朋友可以參考下2016-08-08不用錨點(diǎn)也可以平滑滾動(dòng)到頁面的指定位置實(shí)現(xiàn)代碼
在不適用錨點(diǎn)的情況下也可以平滑滾動(dòng)到頁面的指定位置,看樣子還不錯(cuò)嗎,具體處理程序如下,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-05-05AspNet中使用JQuery boxy插件的確認(rèn)框
Boxy是一個(gè)基于JQuery的彈出層插件,它有相對(duì)漂亮的外觀,功能齊全,支持iframe,支持模式窗口但相對(duì)于同樣的彈出層插件BlockUI它明顯笨重,但使用不那么方便。 兼容瀏覽器:IE6+/Firefox/Google Chrome2015-05-05