ajax校验用户名是否存在

1.前端页面

<input name="USERID" type="text" id="USERID" />
//校验用户名是否存在
<script>
$("#USERID").blur(function(){
var USERID = $(this).val();
$.ajax({
 url: "checkUserName.jsp",
 type: "POST",
 data:{"USERID":USERID},
 dataType: "text",
 async: false,//同步执行
 //error: function(){alert("erro");},
 success: function(data){
 if(data == 1){
 $("#erroInfo").next("span").hide();
 $("#erroInfo").show();
     $("#erroInfo").html("&nbsp;用户名已存在,请重新输入!");
 }else{
 $("#erroInfo").next("span").show();
 $("#erroInfo").hide();
 }
}
});
});
 </script>
 2.java编写后台处理
String USERID=request.getParameter("USERID");
  String strSQL = "select * from d_zwuser where userid = '"+USERID+"'";
  DBService dbservice = new DBService();
  String result = "";
  DBSet dbset = dbservice.getSelectSet(strSQL);
  if(dbset.size() > 0){
 //result = "&nbsp;用户名已存在,请重新输入!";
 result="1";
  }
  response.getWriter().flush();
  response.setCharacterEncoding("utf-8");
  response.getWriter().print(result);
  response.getWriter().flush();

相关推荐