引入
<script type="text/javascript" src="js/jquery-1.8.3.min.js" ></script>
<script type="text/javascript" src="js/jquery.validate.js" ></script>
使用方法
<script type="text/javascript">
$(function(){
$("#form1").validate({
rules:{
uname:"required",
upass:{required:true,digits:true,minlength:6,maxlength:12},
repass:{equalTo:"#upass"},
uage:{max:150,min:0}
},
messages:{
uname:"用户名必填",
upass:{
required:"密码必填",
digits:"密码必须是整数",
minlength:"密码至少6位",
maxlength:"密码不能超过12位"
},
repass:"密码不一致",
uage:{
max:"年龄不能超过{0}",
min:"年龄不能小于0"
}
}
});
})
</script>
<form id="form1" action="index2.html" method="get">
username:<input type="text" name="uname"><br>
password:<input type="text" name="upass" id="upass"><br>
repeatpassword:<input type="text" name="repass"><br>
age:<input type="text" name="uage"><br>
<input type="submit" value="提交">
</form>
若用validate调用后台方法,返回值只能为boolean
<script type="text/javascript">
$(function(){
$("form").validate({
rules:{
yuanpass:{required:true,
remote:"/user/checkpass"},
userPs:{required:true,minlength:6},
newpass2:{required:true,equalTo:"#userPs"}
},
messages:{
yuanpass:{
required:"请输入原密码",
remote:"与原密码不一致"
},
userPs:{
required:"请输入新密码",
minlength:"新密码至少6位"
},
newpass2:{
required:"请输入重复密码",
equalTo:"两次密码要一致"
}
}
})
})
</script>
后台接收数据
//验证密码
@RequestMapping("/user/checkpass")
public void checkpass(String yuanpass,ModelMap map,HttpServletResponse response){
UserTb userTb=(UserTb) map.get("u1");
response.setContentType("text/html;charset=utf-8");
try {
PrintWriter out=response.getWriter();
if(userTb.getUserPs().equals(yuanpass)){
out.print(true);
}else{
out.print(false);
}
} catch (IOException e) {
e.printStackTrace();
}
}