s验证数据库中字段值是否重复
daoImpl:
public String isVipCode(String vipcode) {
String sql = "from FfzjUserEntity where vip_code='"+vipcode+"' ";
Query queryObject = getSession().createQuery(sql.toString());
List<FfzjUserEntity> list = queryObject.list();
if(list.size() > 0) {
//查询出结果 说明数据库中存在
return "false";
}
//反之 数据库中不存在
return "true";
}
controller:
/************************************ 判断会员编号是否存在开始 ******************************/
@RequestMapping(params = "IsVipcode")
@ResponseBody
public void IsVipcode(FfzjUserEntity ffzjUserEntity,
HttpServletRequest req,HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
PrintWriter printWriter = null;
String vipcode = req.getParameter("vipCode");
req.setAttribute("vipcode", vipcode);
String str = "";
if(ffzjUserSerive.isVipCode(vipcode).equals("true")) {
//没有从数据库中查询出数据
str = "false";
}else {
//数据库中已有数据
str = "true";
}try {
printWriter = response.getWriter();
printWriter.print(str);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (null != printWriter) {
printWriter.flush();
printWriter.close();
}
}
}
/************************************ 判断会员编号是否存在结束 ******************************/
jsp:
$(function() {
//设置全局变量str 默认为true
var strd = true;
$("#vipCode").on("blur",function() {
/*================ 验证vipcode start ====================*/
var vipCode = $("#vipCode").val();
var vipCode2 = $("#vipCode2").val();
var params = {"vipCode" : vipCode};
/*与服务器的数据进行交互 start*/
if(vipCode != vipCode2) {
$.ajax( {
type:"post",
data:params,
dataType:"json",
url:"<%=basePath%>/ffzjUser.do?IsVipcode",
success : function(data) {
//data == true说明从数据库中查到了数据
if (data == true){
//将tred设为false
strd = false;
alert("会员编号重复!");
$("#vipCode").focus();
}else {
strd = true;
alert("会员编码可用!");
showMessage("会员编码可用!");
}
}
});
/*与服务器的数据进行交互 end*/
/*================ 验证vipcode end ====================*/
}
});
$("#formobj").Validform(
{
tiptype : 4,
btnSubmit : "#btn_sub",
btnReset : "#btn_reset",
ajaxPost : true,
usePlugin : {
passwordstrength : {
minLen : 6,
maxLen : 18,
trigger : function(obj, error) {
if (error) {
obj.parent().next().find(
".Validform_checktip")
.show();
obj.find(".passwordStrength")
.hide();
} else {
$(".passwordStrength").show();
obj.parent().next().find(
".Validform_checktip")
.hide();
}
}
}
},
callback : function(data) {
var win = frameElement.api.opener;
if (data.success == true) {
frameElement.api.close();
win.tip(data.msg);
} else {
if (data.responseText == ''
|| data.responseText == undefined)
$("#formobj").html(data.msg);
else
$("#formobj").html(data.responseText);
return false;
}
win.reloadTable();
},
beforeCheck:function(curform){
//说明填写的数据与数据库中的重复
if(strd==false) {
alert("会员编号重复!");
$("#vipCode").focus();
return false;
}
var userType = $("#userType").val();
if(userType=="" || userType==null){
alert("用户类型不能为空");
return false;
}
var cardType = $("#cardType").val();
if(cardType=="" || cardType==null){
alert("证件类型不能为空");
return false;
}
var sex = $("#sex").val();
if(sex=="" || sex==null){
alert("请选择性别");
return false;
}
}
});
});