本文主要是介绍jquery.validate自定义验证方法(检验邮箱是否存在),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.前端页面代码
<form method="post" id="registerInfo" action="${ctx }/user/register">
<div class="accountInfoTitle"><div class="accountInfoTitleText">Create Your Account</div><div class="accountInfoTitleLine"></div></div>
<div class="email">
<div class="boxname"><span>*</span>Email:</div>
<div><input type="text" name="email" value="${user.email }" id="email"/></div>
</div>
<div class="submit">
<input type="submit" value="Create My Account" id="registersubmit" οnclick="registerCheck()"/>
</div>
</form>
2.javascript脚本
//注册信息验证
$(function(){
//自定义检验邮箱是否存在的方法
$.validator.addMethod(
"checkEmailIsExsited",
function(value,element,param){
var a=checkEmail(value);
if(a=="1"){
alert(1);
}else{
alert(0);
}
}
);
$("#registerInfo").validate({
submitHandler:function(form){
$.cookie("email", decodeURI($("#email").val()),"{expires:7,secure:true}");
form.submit();
} ,
rules:{
email:{
required:true,
email:true,
remote:{
url: "checkEmail", //后台处理程序
type: "POST", //数据发送方式
dataType: "json", //接受数据格式
data: { //要传递的数据
email: function() {
return $("#email").val();
}
}
}
},
password:{
required:true,
minlength:6
},
repassword:{
required:true,
equalTo: "#password"
},
companyAddress:{
required:true
},
bizContactor:{
required:true
},
companyName:{
required:true
},
phoneNo:{
required:true
},
codeShow:{
required:true
}
},
messages:{
email:{
required:"Account field is required.",
email:"Please enter a valid email address.",
remote:"This Email has been registered, you can <a href='${ctx}/user/toLogin'>[ login ]</a>"
},
password:{
required:"Password field is required.",
minlength:"Password length must be greater than 6."
},
repassword:{
required:"Confirm Password field is required.",
equalTo: "Please enter the same value again."
},
companyAddress:{
required:"Company address field is required."
},
bizContactor:{
required:"BizContactor field is required."
},
companyName:{
required:"Company name field is required."
},
phoneNo:{
required:"Phone number field is required."
},
codeShow:{
required:"Please enter verification code."
}
},
errorElement: "em",
focusInvalid: true,
success: function(em) {
em.text("").addClass("success");
}
});
});
3.后台java代码
/**
* 检验邮箱是否存在
*
* @return
* @author ZuoChangjun 2014-11-27 15:02:54
*/
@RequestMapping({ "/checkEmail" })
public String checkEmail(@RequestParam String email,HttpServletResponse response) {
PrintWriter pw = null;
try {
User user = userService.selectByEmail(email);
pw = response.getWriter();
if (user == null) {
pw.print("true"); //此处需要注意 为字符形式的true/false
} else {
pw.print("false");
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
return ErrorPages._500;
} finally {
if (pw != null) {
pw.close();
}
}
return null;
}
这篇关于jquery.validate自定义验证方法(检验邮箱是否存在)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!