悟生慧

 

Ajax验证输入值是否已存在完整版

  var http_request=false;
    function createRequest()//Ajax验证
    {
      
        try//IE
        {
            http_request=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e2)
            {
                http_request = false;
            }
        }
        if (!http_request && typeof XMLHttpRequest != 'undefined') //FireFox
        {
            http_request = new XMLHttpRequest();
        }
    }

  function yzinsertcallserver()//前台onblur()事件调用此方法
    {
        createRequest();
        var OStadium=document.getElementById("Stadium").value;//一、从前台获取场馆名即获取验证值
        var TStadium = jsTrim(OStadium);//检验值
        var StadiumText = trim(TStadium);//检验值
        if(StadiumText==""||StadiumText==null)              //二、判断如果为空
        {
            var yzm=document.getElementById("reg_Stadium");//获取层的ID
            yzm.className="Error";
            yzm.innerHTML='<img src=../images/check_error.gif>请填写场馆名。';//三、显示 在层上显示请填写
            return false;
        } 
        else
        {            
            var url="yzStadiumName.aspx?yzStadiumName="+escape(StadiumText);//四、否则不为空把参数 传到 这个页面根据名称去数据库中查询 如果有数据
            http_request.onreadystatechange=yzinsertdatepage;//调用方法
            http_request.open("GET",url,true);
            http_request.send(null);

        }
    }
    function yzinsertdatepage()
    {  
        var obj=document.getElementById("reg_Stadium");//前台隐藏层的ID
        if(http_request.readyState == 4)
        {
            if(http_request.status==200)
            {
                var response=http_request.responseText;//获取yzStadiumName.aspx页面的内容 如果是regokStadiumName验证通过
                if(response.indexOf("regokStadiumName")!=-1)//
                {
                    //验证通过
                    obj.className="Right";
                    obj.innerHTML="<img src='../images/check_right.gif'/>";
                    return true;
                }
               else
               {
                    //验证未通过
                     obj.className="Error";
                     obj.innerHTML='<img src=../images/check_error.gif>场馆名已经存在,请重新输入。';
                     return false;
               }
            }
        }
        else
        {   //异步传输失败
            obj.className="Error"
            obj.innerHTML='<img src=../images/check_error.gif>未能检测,请重新输入。';
            return false;
  
        }
    }

 

 

 

//检查获取到的值
    function jsTrim(str){
        str += "";
        while ((str.charAt(0) == ' ') || (str.charAt(0) == '???') || (escape(str.charAt(0)) == '%u3000'))
            str = str.substring(1, str.length);
        while ((str.charAt(str.length - 1) == ' ') || (str.charAt(str.length - 1) == '???') || (escape(str.charAt(str.length - 1)) == '%u3000'))
            str = str.substring(0, str.length - 1);
        return str;
    }
    function trim(inputString){
        if (typeof inputString != "string") {
            return inputString;
        }
        var retValue = inputString;
        var ch = retValue.substring(0, 1);
        while (ch == " ") {
            //检查字符串开始部分的空格
            retValue = retValue.substring(1, retValue.length);
            ch = retValue.substring(0, 1);
        }
        ch = retValue.substring(retValue.length - 1, retValue.length);
        while (ch == " ") {
            //检查字符串结束部分的空格
            retValue = retValue.substring(0, retValue.length - 1);
            ch = retValue.substring(retValue.length - 1, retValue.length);
        }
        while (retValue.indexOf(" ") != -1) {
            //将文字中间多个相连的空格变为一个空格
            retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ") + 1, retValue.length);
        }
        return retValue;
    }

posted on 2010-07-02 16:15  悟生慧  阅读(852)  评论(0编辑  收藏  举报

导航