文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Ajax基础教程学习(2.1)_完成验证

Ajax基础教程学习(2.1)_完成验证

时间:2010-08-31  来源:快乐的Tina

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >

<html> <head>    <title>Using Ajax for validation</title>    <script type="text/javascript">      
 var xmlHttp;       
 function createXMLHttpRequest() {           
    if (window.ActiveXObject) {               
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");           
    }else if (window.XMLHttpRequest) {              
        xmlHttp = new XMLHttpRequest();           
    }       
 }      
 
 function validate() {           
    createXMLHttpRequest();         
    var date = document.getElementByIdx_x("birthDate");          
    var url = "Handler.ashx?birthDate=" + escape(date.value);           
    xmlHttp.open("GET", url, true);           
    xmlHttp.onreadystatechange = callback;           
    xmlHttp.send(null);       
 }       
   
    function callback() {           
        if (xmlHttp.readyState == 4) {          
         if (xmlHttp.status == 200) {                  
             var mes =xmlHttp.responseXML.getElementsByTagName_r("message")[0].firstChild.data;          
             var val =xmlHttp.responseXML.getElementsByTagName_r("passed")[0].firstChild.data;
             setMessage(mes, val);               
          }          
        }       
     }  
                  
function setMessage(message, isValid) {          
    var messageArea = document.getElementByIdx_x("dateMessage");           
    var fontColor = "red";         
    if (isValid) {            
        fontColor = "green"; 
                     
        }           
    messageArea.innerHTML = "<font color=" + fontColor + ">" + message + " </font>";      
     }   
    
</script> </head> <body>    <h1>Ajax Validation Example</h1>    Birth date: <input type="text" size="10" id="birthDate" onchange="validate();"/>    <div id="dateMessage"></div> </body>

</html>

服务端代码

 

<%@ WebHandler Language="C#" class="Handler" %>

using System;
using System.Web;
using System.Text;
using System.Collections;

public class Handler : IHttpHandler {
    public void ProcessRequest (HttpContext context) {
        bool pass = Validate(context.Request.QueryString["birthDate"]);
        context.Response.ContentType = "text/xml";
        context.Response.AddHeader("Cache-Control","no-cache");
        String message = "您输入的日期不正确.";
        if (pass)
        {
            message = "您输入的日期正确.";
        }
        context.Response.Write("<response>");
        context.Response.Write("<passed>"+pass+"</passed>");
        context.Response.Write("<message>" + message + "</message>");
        context.Response.Write("</response>");
        context.Response.End();
           
      
    }

    public bool Validate(string date)
    {
        bool isValid = true;
        if (date != null)
        {
            try
            {
                DateTime.Parse(date);
            }
            catch (Exception)
            {
                isValid = false;
            }
        }
        else
        {
            isValid = false;
        }

        return isValid;
       
    }
       
       
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载