JQuery validate插件驗(yàn)證用戶注冊信息
使用JQuery的validate插件做客戶端驗(yàn)證非常方便,下面做一個(gè)使用validate插件驗(yàn)證用戶注冊信息的例子。
本實(shí)例使用的是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"><!-- //擴(kuò)展validator的校驗(yàn)方法 $.validator.addMethod("onlyLetterAndDigit",function(value, element, params){ var regex=new RegExp('^[0-9a-zA-Z]+$'); return regex.test(value); },"只能輸入字母或數(shù)字"); $(function(){ $("#registe").validate({ //定義驗(yàn)證規(guī)則,其中屬性名為表單的name屬性 rules:{ username:{ required:true, onlyLetterAndDigit:true,//使用自定義方法限制只能輸入字母或數(shù)字 rangelength:[4,20], remote:"registe!validName.action"http://使用AJAX異步校驗(yàn) }, 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:"請?jiān)俅屋斎朊艽a", equalTo:"密碼輸入不一致,請重新輸入" }, email:{ required:"請輸入電子郵件", email:"請輸入合法的電子郵件" }, vercode:{ required:"請輸入驗(yàn)證碼" } } }); }); //刷新驗(yàn)證碼 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>確認(rèn)密碼:</td><td><input type="text" name="chkpassword"/></td> </tr> <tr> <td>Email:</td><td><input type="text" name="email"/></td> </tr> <tr> <td>驗(yàn)證碼:</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">刷新驗(yàn)證碼</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); //判斷驗(yàn)證碼是否正確 if (vercode.equals(ver2)) { if (userManager.validName(username)) { if (userManager.addUser(username, password, email) > 0) return SUCCESS; else addActionError("注冊失敗,請重試!"); } else { addActionError("該用戶名已存在,請重新輸入!"); } } else { addActionError("驗(yàn)證碼不匹配,請重新輸入"); } return INPUT; } //驗(yàn)證用戶名是否可用 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(); // 因?yàn)橹苯虞敵鰞?nèi)容而不經(jīng)過jsp,因此返回null. return null; }
效果圖如下:
注意:使用remote異步驗(yàn)證用戶名的方法應(yīng)該通過response.getWriter().write("true")來輸出,而不能像普通方法一樣返回字符串。
關(guān)于插件更詳細(xì)的介紹可以查看“jQuery validate驗(yàn)證插件使用詳解”。
另外,jQuery也支持動態(tài)給控件添加校驗(yàn),例如:
但要注意:如果對集合中的元素動態(tài)添加校驗(yàn)需要循環(huán)對每個(gè)元素添加,這是因?yàn)閖Query隱式實(shí)現(xiàn)了集合操作,但validate插件沒有。例如:
$(".quantity").each(function(){ $(this).rules("add",{digits:true,required:true}); });
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
- jquery validate表單驗(yàn)證插件
- 基于Bootstrap+jQuery.validate實(shí)現(xiàn)表單驗(yàn)證
- jQuery驗(yàn)證插件validate使用方法詳解
- CKEditor無法驗(yàn)證的解決方案(js驗(yàn)證+jQuery Validate驗(yàn)證)
- jQuery validate+artdialog+jquery form實(shí)現(xiàn)彈出表單思路詳解
- jquery.validate提示錯誤信息位置方法
- jQuery validate插件實(shí)現(xiàn)ajax驗(yàn)證重復(fù)的2種方法
- jQuery validate插件submitHandler提交導(dǎo)致死循環(huán)解決方法
- 深入學(xué)習(xí)jQuery Validate表單驗(yàn)證(二)
- jquery.validate[.unobtrusive]和Bootstrap實(shí)現(xiàn)tooltip錯誤提示問題分析
相關(guān)文章
jQuery實(shí)現(xiàn)手風(fēng)琴效果(蒙版)
這篇文章主要介紹了jQuery實(shí)現(xiàn)手風(fēng)琴效果,附帶蒙版特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01jQuery插件Skippr實(shí)現(xiàn)焦點(diǎn)圖幻燈片特效
Skippr 是一個(gè)超級簡單的 jQuery 幻燈片插件。只是包括你的網(wǎng)頁中引入 jquery.skippr.css 和 jquery.skippr.js 文件就能使用了。Skippr 能夠自適應(yīng)窗口寬度,而且導(dǎo)航是獨(dú)特的條形導(dǎo)航。2015-04-04那些年,我還在學(xué)習(xí)jquery 學(xué)習(xí)筆記
那些年學(xué)習(xí)了一些基本的web開發(fā)知識,其中已經(jīng)有javascript語言了,為什么還要學(xué)習(xí)Jquery啊2012-03-03jquery禁止輸入數(shù)字以外的字符的示例(純數(shù)字驗(yàn)證碼)
這篇文章主要介紹了jquery禁止輸入數(shù)字以外的字符的示例(純數(shù)字驗(yàn)證碼),需要的朋友可以參考下2014-04-04jQuery插件cxSelect多級聯(lián)動下拉菜單實(shí)例解析
這篇文章主要為大家詳細(xì)解析了jQuery插件cxSelect多級聯(lián)動下拉菜單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06基于Jquery的跨域傳輸數(shù)據(jù)(JSONP)
基于Jquery的跨域傳輸數(shù)據(jù)(JSONP) ,需要的朋友可以參考下。2011-03-03跟我一起學(xué)寫jQuery插件開發(fā)方法(附完整實(shí)例及下載)
jQuery如此流行,各式各樣的jQuery插件也是滿天飛。你有沒有想過把自己的一些常用的JS功能也寫成jQuery插件呢?如果你的答案是肯定的,那么來吧!和我一起學(xué)寫jQuery插件吧!2010-04-04基于jquery實(shí)現(xiàn)的服務(wù)器驗(yàn)證控件的啟用和禁用代碼
用戶點(diǎn)擊下一步時(shí),不對Display=none的新增區(qū)域表單進(jìn)行驗(yàn)證,需要在用戶點(diǎn)擊“取消增加時(shí)”,禁用服務(wù)器驗(yàn)證控件。反之,啟用服務(wù)器驗(yàn)證控件。2010-04-04