前端
//初始化工作
$(document).ready(function(){
//对表单进行初始化工作,定义校验成功和校验失败的逻辑 //待写 遮罩层 ...
$.formValidator.initConfig({formid:"registerForm",onerror:function(msg){},onsuccess:function(){return true;}});
//用户名校验 表单校验器.输入校验器.正则校验器.ajax校验器(光标失焦的时候触发的)
$("#loginName").formValidator({onshow:"请输入用户名",
onfocus:"6-20位字母、数字、下划线组成",oncorrect:"该用户名可以注册"}).inputValidator({min:6,max:20,onerror:"用户名长度不满足要求"}).regexValidator({regexp:"username",datatype:"enum",onerror:"用户名格式不正确"})
.ajaxValidator({
type : "post",
url : "${ctx}/login/checkLoginName",
data:{"loginName" : function(){return $("#loginName").val();}},
success : function(data){
if( data == "1" )
{
return true;
}
else
{
return false;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
onerror : "此用户名已被注册",
onwait : "正在对用户名进行合法性校验,请稍候..."
});
//手机号
$("#telnum").formValidator({onshow:"请输入手机号",onfocus:"输入的手机号必须合法",oncorrect:"该手机号可以注册"}).inputValidator({min:11,max:11,onerror:"手机号码必须是11位的哦"}).regexValidator({regexp:"mobile",datatype:"enum",onerror:"手机号格式不正确"})
.ajaxValidator({
type : "post",
url : "${ctx}/login/checkPhone",
data:{"telnum" : function(){return $("#telnum").val();}},
success : function(data){
if( data == "1" )
{
return true;
}
else
{
return false;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
onerror : "此手机号已被注册",
onwait : "正在对手机号进行合法性校验,请稍候..."
});
//邮箱
$("#email").formValidator({onshow:"请输入邮箱",onfocus:"此邮箱用来找回密码",oncorrect:"该邮箱可以注册"}).regexValidator({regexp:"email",datatype:"enum",onerror:"邮箱格式不正确"})
.ajaxValidator({
type : "post",
url : "${ctx}/login/checkEmail",
data:{"email" : function(){return $("#email").val();}},
success : function(data){
if( data == "1" )
{
return true;
}
else
{
return false;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
onerror : "此邮箱已被注册",
onwait : "正在对邮箱进行合法性校验,请稍候..."
});
//密码
$("#password1").formValidator({onshow:"请输入密码",onfocus:"密码必须为6位以上哦",oncorrect:"密码合法"}).inputValidator({min:6,empty:{leftempty:false,rightempty:false,emptyerror:"密码两边不能有空符号"},onerror:"密码不能为空,请确认"});/* .regexValidator({regexp:"password",datatype:"enum",onerror:"密码最少长度为 6位 ,并至少包含2种复杂类别的字符"}); */
$("#password2").formValidator({onshow:"请输入确认密码",onfocus:"两次密码必须一致哦",oncorrect:"密码一致"}).inputValidator({min:1,empty:{leftempty:false,rightempty:false,emptyerror:"重复密码两边不能有空符号"},onerror:"重复密码不能为空,请确认"}).compareValidator({desid:"password1",operateor:"=",onerror:"2次密码不一致,请确认"});
//企业名称
$("#companyname").formValidator({onshow:"请输入企业名称",onfocus:"请输入企业名称",oncorrect:"该企业名可以注册"}).inputValidator({min:6,onerror:"企业名称长度不满足要求"})
.ajaxValidator({
type : "post",
url : "${ctx}/login/checkCompanyname",
data:{"companyname" : function(){return $("#companyname").val();}},
success : function(data){
if( data == "1" )
{
return true;
}
else
{
return false;
}
},
buttons: $("#button"),
error: function(){alert("服务器没有返回数据,可能服务器忙,请重试");},
onerror : "该企业已被注册过",
onwait : "正在对企业名进行合法性校验,请稍候..."
});
//qq
$("#qq").formValidator({onshow:"请输入qq号码",onfocus:"请输入qq号码",oncorrect:"qq号码有效"}).inputValidator({min:6,onerror:"qq号码长度不满足要求"}).regexValidator({regexp:"qq",datatype:"enum",onerror:"qq号码格式不正确"});
//汽配商联系人
$("#username").formValidator({onshow:"请输入汽配商联系人",onfocus:"请输入汽配商联系人",oncorrect:"格式正确"}).inputValidator({min:2,onerror:"不能少于2个字符"});
});
`以邮箱为例子。后端
@RequestMapping("/checkEmail")
public void checkEmail(HttpServletResponse response,String email) throws IOException {
SysUser user=userService.findUserByEmail(email);
if (null == user) {
response.getWriter().write("1");
} else {
response.getWriter().write("2");
}
}
需要的js文件
链接: https://pan.baidu.com/s/1dYFzF-codJGmGqPkM2J_Hw 提取码: 6pi8 复制这段内容后打开百度网盘手机App,操作更方便哦