===========A:单独使用JQuery Ajax功能(随意返回各种类型数值)===========================
/**注册-验证手机号是否已存在*/
function validateTelID() {
var telID = $("#telID").val();
$.ajax({type:"POST", url:"Regedit_validateTelID", data:"telID=" + telID, success:function (data) {
if (data == 0) {
//0为不存在,提交注册
} else {
// 验证手机号存在(1为存在,留在当前页面)
}
}});
}
==========B:在JQuery的validate功能中使用remote实现Ajax功能后台验证(只可返回String类型"true"或"false"值)===
<script type="text/javascript">
$.validator.setDefaults({
submitHandler: function(regeditForm)
{
alert("提交ok");
//document.regeditForm.action="Regedit_regeditUserStep2"
// document.regeditForm.submit();
}
});
$().ready(function() {
$("#regeditForm").validate({
rules: {
confirmCode: {
required: true,
remote: {
type:"POST",
url:"Regedit_validateConfirmCode",
data:{
confirmCode: function() {return $("#confirmCode").val();}
}
}
},
userPwd: {
required: true,
minlength: 2
},
confirmUserPwd: {
required: true,
minlength: 2,
equalTo: "#userPwd"
}
},
messages: {
confirmCode: {
required: "<font color=/"#ff0000/">请填写验证码</font>",
remote:"验证码错误了"
},
userPwd: {
required: "<font color=/"#ff0000/">请填写密码</font>",
minlength: "<font color=/"#ff0000/">密码长度不得小于5位</font>"
},
confirmUserPwd: {
required: "<font color=/"#ff0000/">请重复输入密码</font>",
minlength: "<font color=/"#ff0000/">密码长度不得小于5位</font>",
equalTo: "<font color=/"#ff0000/">确认密码与密码不一致</font>"
}
}
});
});
</script>
b中使用remote实现ajax ,只需注意后台action只能返回"true"或"false"
前端不用进行再次判断
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String confirmCode = request.getParameter("confirmCode").trim();
response.setContentType("text/html");
String value = "false";
response.getWriter().write(value);
本文介绍两种使用JQuery Ajax实现后台验证的方法:一种是直接使用JQuery Ajax功能,可以返回任意类型的数值;另一种是在JQuery的validate插件中使用remote选项实现Ajax验证,此时后台只能返回true或false。
169

被折叠的 条评论
为什么被折叠?



