table

          ajax檢測用戶名(email格式的用戶名)

          <input name=email class=clear_input id=email maxlength="50" value="<%=email%>" onBlur="javascript:checkEnglish()"/>
          <span id=reg_email_error style="DISPLAY: none">ReadyState</span>

          function checkEnglish(){
          if (!CheckIfEnglish(document.insertForm.email.value )) {
             alert("請輸入數字,字母和下劃線!");
             document.insertForm.email.value="";
             document.insertForm.email.focus();
             return false;
          }
          else {
             doCheck();
             return true;
          }
          }

          function CheckIfEnglish( String ){
                var Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_";
                var i;
                var c;
               if(String.charAt( 0 )=='-')
                  return false;
               if( String.charAt( String.length - 1 ) == '-' )
                     return false;
               for( i = 0; i < String.length; i ++ ){
                     c = String.charAt( i );
                     if (Letters.indexOf( c ) < 0)
                     return false;
               }
               return true;
          }

          ajax.js

          //定義XMLHttpRequest對象實例
          var http_request = false;
          //定義可復用的http請求發送函數
          function send_request(method,url,content,responseType,callback) {//初始化、指定處理函數、發送請求的函數
               http_request = false;
               //開始初始化XMLHttpRequest對象
               if(window.XMLHttpRequest) { //Mozilla 瀏覽器
                   http_request = new XMLHttpRequest();
                   if (http_request.overrideMimeType) {//設置MiME類別
                       http_request.overrideMimeType("text/xml");
                   }
               }
               else if (window.ActiveXObject) { // IE瀏覽器
                   try {
                       http_request = new ActiveXObject("Msxml2.XMLHTTP");
                   } catch (e) {
                       try {
                           http_request = new ActiveXObject("Microsoft.XMLHTTP");
                       } catch (e) {}
                   }
               }
               if (!http_request) { // 異常,創建對象實例失敗
                   window.alert("不能創建XMLHttpRequest對象實例.");
                   return false;
               }
               if(responseType.toLowerCase()=="text") {
                   //http_request.onreadystatechange = processTextResponse;
                   http_request.onreadystatechange = callback;
               }
               else if(responseType.toLowerCase()=="xml") {
                   //http_request.onreadystatechange = processXMLResponse;
                   http_request.onreadystatechange = callback;
               }
               else {
                   window.alert("響應類別參數錯誤。");
                   return false;
               }
               // 確定發送請求的方式和URL以及是否異步執行下段代碼
               if(method.toLowerCase()=="get") {
                   http_request.open(method, url, true);
               }
               else if(method.toLowerCase()=="post") {
                   http_request.open(method, url, true);
                   http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
               }
               else {
                   window.alert("http請求類別參數錯誤。");
                   return false;
               }
               http_request.send(content);
          }
          // 處理返回文本格式信息的函數
          function processTextResponse() {
               if (http_request.readyState == 4) { // 判斷對象狀態
                   if (http_request.status == 200) { // 信息已經成功返回,開始處理信息
                       //alert(http_request.responseText);
                       alert("Text文檔響應。");
                   } else { //頁面不正常
                       alert("您所請求的頁面有異常。");
                   }
               }
          }
          //處理返回的XML格式文檔的函數
          function processXMLResponse() {
               if (http_request.readyState == 4) { // 判斷對象狀態
                   if (http_request.status == 200) { // 信息已經成功返回,開始處理信息
                       //alert(http_request.responseXML);
                       alert("XML文檔響應。");
                   } else { //頁面不正常
                       alert("您所請求的頁面有異常。");
                   }
               }
          }

          function doCheck() {
                var f = document.forms[0];
                   if(f.email.value!="") {
                       document.getElementById("reg_email_error").innerHTML = "系統正在處理您的請求,請稍后。";
                       send_request("GET","checkUsername.jsp?email="+f.email.value,null,"text",showFeedbackInfo);
                   }
                   else {
                       document.getElementById("reg_email_error").innerHTML = "請輸入用戶名稱。";
                   }    
          }

          checkUsername.jsp

          <%@ page contentType="text/html; charset=gbk"%>
          <%@ page import="com.beyoung.blog.hql.admin.huiyuangguanli.HuiyuangguanliHql" %>
          <%@ page import="com.beyoung.blog.bean.Huiyuangguanli" %>
          <%@ page import="com.beyoung.blog.util.strDate.PubFun" %>
          <%
          String name=request.getParameter("email");
          name=PubFun.toGBK(name);
          System.out.println(name);
          System.out.println(" :"+HuiyuangguanliHql.getHuiyuangguanliHql().getHuiyuangguanliKey(name));
          if(!HuiyuangguanliHql.getHuiyuangguanliHql().getHuiyuangguanliKey(name)){  
             out.println("<font color='#FF0000'>用戶名稱["+name+"]已占用!</font>");
          }
          else{
          out.println("<font color='#088EOF'>用戶名稱["+name+"]尚未被注冊,您可以繼續。</font>");
          }
          %>

          posted on 2008-12-15 11:31 小卓 閱讀(905) 評論(3)  編輯  收藏 所屬分類: html and js

          Feedback

          # re: ajax檢測用戶名(email格式的用戶名) 2011-04-11 23:59 陳龍超

          呵呵  回復  更多評論   

          # 一動不動 2013-06-02 22:05

          震天二壓下壓下  回復  更多評論   

          # 一動不動 2013-06-02 22:06

          對于  回復  更多評論   

          主站蜘蛛池模板: 凉山| 龙井市| 资兴市| 安吉县| 长武县| 常山县| 青河县| 黑水县| 玉龙| 蓬溪县| 香河县| 商城县| 平安县| 阿城市| 冀州市| 邯郸县| 舟山市| 哈尔滨市| 南城县| 阿城市| 额敏县| 临高县| 炉霍县| 兴化市| 三河市| 昭觉县| 肇东市| 格尔木市| 肥城市| 民勤县| 石门县| 乡宁县| 隆昌县| 和政县| 许昌县| 班戈县| 壶关县| 资源县| 温泉县| 合作市| 屏东县|