JQuery validate插件驗證用戶注冊信息
使用JQuery的validate插件做客戶端驗證非常方便,下面做一個使用validate插件驗證用戶注冊信息的例子。
本實例使用的是1.5版本。
示例是在SSH下做的,代碼如下:
registe.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注冊頁面</title> <mce:script type="text/javascript" src="js/jquery.1.4.2.js" mce_src="js/jquery.1.4.2.js"></mce:script> <mce:script type="text/javascript" src="js/validate/jquery.validate.js" mce_src="js/validate/jquery.validate.js"></mce:script> <link href="js/validate/jquery.validate.css" mce_href="js/validate/jquery.validate.css" type="text/css" rel="stylesheet"/> <mce:script type="text/javascript"><!-- //擴展validator的校驗方法 $.validator.addMethod("onlyLetterAndDigit",function(value, element, params){ var regex=new RegExp('^[0-9a-zA-Z]+$'); return regex.test(value); },"只能輸入字母或數(shù)字"); $(function(){ $("#registe").validate({ //定義驗證規(guī)則,其中屬性名為表單的name屬性 rules:{ username:{ required:true, onlyLetterAndDigit:true,//使用自定義方法限制只能輸入字母或數(shù)字 rangelength:[4,20], remote:"registe!validName.action"http://使用AJAX異步校驗 }, password:{ required:true, rangelength:[4,20] }, chkpassword:{ required:true, equalTo:"#password" }, email:{ required:true, email:true }, vercode:"required" }, messages:{ username:{ required:"請輸入用戶名", rangelength:"用戶名長度必須在4~20位之間", remote:$.format("用戶名{0}已存在,請重新輸入!") }, password:{ required:"請輸入密碼", rangelength:"密碼長度必須在4~20位之間" }, chkpassword:{ required:"請再次輸入密碼", equalTo:"密碼輸入不一致,請重新輸入" }, email:{ required:"請輸入電子郵件", email:"請輸入合法的電子郵件" }, vercode:{ required:"請輸入驗證碼" } } }); }); //刷新驗證碼 function refresh() { $("#authImg").src="authImg?now="+new Date(); } // --></mce:script> </head> <body> <form action="registe.action" method="post" id="registe"> <table> <caption><h2>用戶注冊</h2></caption> <tr> <td>用 戶 名:</td><td><input type="text" name="username" id="username"/></td> </tr> <tr> <td>密 碼:</td><td><input type="text" name="password" id="password"/> </td> </tr> <tr> <td>確認密碼:</td><td><input type="text" name="chkpassword"/></td> </tr> <tr> <td>Email:</td><td><input type="text" name="email"/></td> </tr> <tr> <td>驗證碼:</td><td valign="bottom"><input type="text" name="vercode" size="10"/> <img alt="" src="authImg" mce_src="authImg" id="authImg" align="absmiddle"><a href="#" mce_href="#" onclick="refresh()"><span style="font-size:12px" mce_style="font-size:12px">刷新驗證碼</span></a></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交"/><input type="reset" value="重填"/></td> </tr> </table> </form> </body> </html>
后臺RegisteAction.java的主要方法
public String execute() throws Exception { Map session = ActionContext.getContext().getSession(); String ver2 = (String) session.get("rand"); session.put("rand", null); //判斷驗證碼是否正確 if (vercode.equals(ver2)) { if (userManager.validName(username)) { if (userManager.addUser(username, password, email) > 0) return SUCCESS; else addActionError("注冊失敗,請重試!"); } else { addActionError("該用戶名已存在,請重新輸入!"); } } else { addActionError("驗證碼不匹配,請重新輸入"); } return INPUT; } //驗證用戶名是否可用 public String validName() throws Exception { System.out.println(username); boolean flag = userManager.validName(username); HttpServletResponse response = ServletActionContext.getResponse(); response.setDateHeader("Expires", 0); response.addHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setContentType("text/plain;charset=UTF-8"); if (flag) response.getWriter().write("true"); else response.getWriter().write("false"); response.getWriter().flush(); // 因為直接輸出內(nèi)容而不經(jīng)過jsp,因此返回null. return null; }
效果圖如下:
注意:使用remote異步驗證用戶名的方法應該通過response.getWriter().write("true")來輸出,而不能像普通方法一樣返回字符串。
關(guān)于插件更詳細的介紹可以查看“jQuery validate驗證插件使用詳解”。
另外,jQuery也支持動態(tài)給控件添加校驗,例如:
但要注意:如果對集合中的元素動態(tài)添加校驗需要循環(huán)對每個元素添加,這是因為jQuery隱式實現(xiàn)了集合操作,但validate插件沒有。例如:
$(".quantity").each(function(){ $(this).rules("add",{digits:true,required:true}); });
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。
- jquery validate表單驗證插件
- 基于Bootstrap+jQuery.validate實現(xiàn)表單驗證
- jQuery驗證插件validate使用方法詳解
- CKEditor無法驗證的解決方案(js驗證+jQuery Validate驗證)
- jQuery validate+artdialog+jquery form實現(xiàn)彈出表單思路詳解
- jquery.validate提示錯誤信息位置方法
- jQuery validate插件實現(xiàn)ajax驗證重復的2種方法
- jQuery validate插件submitHandler提交導致死循環(huán)解決方法
- 深入學習jQuery Validate表單驗證(二)
- jquery.validate[.unobtrusive]和Bootstrap實現(xiàn)tooltip錯誤提示問題分析
相關(guān)文章
jQuery插件Skippr實現(xiàn)焦點圖幻燈片特效
Skippr 是一個超級簡單的 jQuery 幻燈片插件。只是包括你的網(wǎng)頁中引入 jquery.skippr.css 和 jquery.skippr.js 文件就能使用了。Skippr 能夠自適應窗口寬度,而且導航是獨特的條形導航。2015-04-04jquery禁止輸入數(shù)字以外的字符的示例(純數(shù)字驗證碼)
這篇文章主要介紹了jquery禁止輸入數(shù)字以外的字符的示例(純數(shù)字驗證碼),需要的朋友可以參考下2014-04-04jQuery插件cxSelect多級聯(lián)動下拉菜單實例解析
這篇文章主要為大家詳細解析了jQuery插件cxSelect多級聯(lián)動下拉菜單實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-06-06基于Jquery的跨域傳輸數(shù)據(jù)(JSONP)
基于Jquery的跨域傳輸數(shù)據(jù)(JSONP) ,需要的朋友可以參考下。2011-03-03跟我一起學寫jQuery插件開發(fā)方法(附完整實例及下載)
jQuery如此流行,各式各樣的jQuery插件也是滿天飛。你有沒有想過把自己的一些常用的JS功能也寫成jQuery插件呢?如果你的答案是肯定的,那么來吧!和我一起學寫jQuery插件吧!2010-04-04基于jquery實現(xiàn)的服務器驗證控件的啟用和禁用代碼
用戶點擊下一步時,不對Display=none的新增區(qū)域表單進行驗證,需要在用戶點擊“取消增加時”,禁用服務器驗證控件。反之,啟用服務器驗證控件。2010-04-04