ajax检测用户是否存在

ajax检查用户名异步实现:

<script type="text/javascript">
  <!--
      var http_request;
              function reader()
              {
                  if (window.XMLHttpRequest)
     { // if Mozilla, Safari etc
       http_request=new XMLHttpRequest()
       if (http_request.overrideMimeType)
         http_request.overrideMimeType('text/xml');
      }
      else if (window.ActiveXObject)
      { // if IE
        try {
          http_request=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
          try{
             http_request=new ActiveXObject("Microsoft.XMLHTTP");
          }
          catch (e){}
        }
      }

                 var userName=document.regForm.coUserName.value;
                 http_request.onreadystatechange=process;
                 //servlet文件名一定要全小写
                 http_request.open("GET","checkCoUserName.do?userName="+userName);
                 http_request.send(null); 
                 

              }
          function process()
          {
             if(http_request.readyState==4)
             {
               var chkResult = http_request.responseText;
                
               if(chkResult==1)
               {   
                document.getElementById("chkName").innerHTML="<font color='red'>恭喜用户名可以使用!</font>";
               }
               else
               {
                 document.getElementById("chkName").innerHTML="<font color='red'>此用户名已经存在!请换一个用户名!</font>";
               }
           
              }
          }
     -->
</script>

注意:当失去焦点时,发送信息 οnblur="reader()" ,reader里面有发送请求.do的字duan,
Struts action中只要out.pritnln(1)或0,然后forward可以为空!这样就行了。
注意:提交的信息给一个action处理,异步检查用户名的给另一个action处理!两个action!

<html>

<td align="left" valign="middle" background="images/regist_CoUser_bj01.jpg" class="white">&nbsp;&nbsp;&nbsp;&nbsp;
 <img src="images/regist_pic04.gif" width="10" height="10"> 用户登陆
 <span class="style1">*</span></td>
  <td align="left" valign="middle"  background="images/regist_CoUser_bj02.jpg" class="black">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  <input name="coUserName" type="text" size="20" maxlength="20"  οnblur="reader()" />
</html>


Struts action:


public ActionForward execute(
   ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) throws IOException {
  String sucTarget = "";
  String failTarget = "regFail";
  PrintWriter out = response.getWriter();
  String coUserName = request.getParameter("userName");  //获得传过来的值
  RegistControlBean regConBean = new RegistControlBean();
  if(!regConBean.isExistCoUserName(coUserName)) { //用户名不存在,即可用
   out.println("1");
  }else {
   out.println("2");
  }
  return mapping.findForward("");
 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值