欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JQuery validate插件驗(yàn)證用戶注冊信息

 更新時(shí)間:2016年05月11日 15:09:32   作者:cuisea  
這篇文章主要為大家詳細(xì)介紹了JQuery validate插件驗(yàn)證用戶注冊信息的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用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),例如:

復(fù)制代碼 代碼如下:
("#email").rules("add", { required: true, email: true }); 

但要注意:如果對集合中的元素動態(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í)有所幫助。

相關(guān)文章

最新評論