• ajax学习笔记简单事例1完成验证


    这个例子很简单,有一个输入框,onchange()事件触发验证.
    html:

    <HTML>
        
    <HEAD>
            
    <title>default</title>
            
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
            
    <meta name="CODE_LANGUAGE" Content="C#">
            
    <meta name="vs_defaultClientScript" content="JavaScript">
            
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
            
        
    </HEAD>
        
    <body MS_POSITIONING="GridLayout">
            
    <form id="Form1" method="post" runat="server">
                birthDate
                
                
    <input type=text id="birthDate" onchange="validate();"/>
            
                
    <div id="dateMessage"></div>
            
    </form>
            
    <script language="javascript">
    var xmlHttp;

     
    function validate()
     {
     
      
       
    if(window.ActiveXObject)
       {
          xmlHttp
    =new ActiveXObject("Microsoft.XMLHTTP");
       }
       
    else if(window.XMLHttpRequest)
       {
         xmlHttp
    =new XMLHttpRequest();
       }
     
      
    var date=document.getElementById("birthDate");
      
    var url="validation.aspx?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("message")[0].firstChild.data;
          
    var val=xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;
         setMessage(mes,val);
       }
      }
     }
     
    function setMessage(message,isValid)
     {
       
       
    var messageArea=document.getElementById("dateMessage");
       
    var fontColor="red";
       
    if(isValid=="true")
       {
         fontColor
    ="green";
         
       }
       messageArea.innerHTML
    ="<font color="+fontColor+" >"+message+"</font>";
     }
            
    </script>
        
    </body>
    </HTML>

     validation.cs:

    private void Page_Load(object sender, System.EventArgs e)
            {
                
    bool passed=validateDate(Request.QueryString["birthDate"].ToString());

                
    //this.Response.ContentType = "application/xml"; //
                this.Response.ContentType = "text/xml";
                
    //Response.Charset = "GB2312";
                this.Response.AppendHeader("Cache-Control","no-cache");
                
    string message="You have entered an invalid date";
                
    if(passed)
                {
                    message
    ="You have entered a valid date.";
                }

                
    this.Response.Write("<response>");
                
    this.Response.Write("<passed>"+passed.ToString().ToLower()+"</passed>");
                
    this.Response.Write("<message>"+message+"</message>");
                
    this.Response.Write("</response>");
              
    this.Response.End();

            }
            
    private bool validateDate(string date)
            {
                
                    
    try
                    {
                       Convert.ToDateTime(date);
                        
    return true;
                    }
                    
    catch
                    {
                        
    return false;
                    }
                
                
            }
  • 相关阅读:
    HDU-6801 2020HDU多校第三场T11 (生成函数)
    [HDU-6791] 2020HDU多校第三场T1(回文自动机)
    回文自动机 (PAM,Palindrome Automaton)
    字符串的Period(周期),Border
    「APIO2019」路灯 (K-D Tree / 树套树 / CDQ + 树状数组)
    「APIO2019」桥梁(询问分块+并查集)
    「APIO2019」奇怪装置
    「APIO2018」选圆圈(K-D Tree/CDQ+Set)
    堆小结
    【[HNOI/AHOI2018]毒瘤】
  • 原文地址:https://www.cnblogs.com/jasonduan/p/579154.html
Copyright © 2020-2023  润新知