这个例子很简单,有一个输入框,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>
<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;
}
}
{
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;
}
}