您的位置:首页 > 其它

Ajax自动校验的表单

2010-09-08 00:17 246 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>自动校验的表单</title>
<style type="text/css">
<!--
form{
padding:0px; margin:0px;
font-size:12px;
font-family:Arial, Helvetica, sans-serif;
}
input{
border:1px solid #004082;
font-size:12px;
font-family:Arial, Helvetica, sans-serif;
}
-->
</style>
<script language="javascript">
var xmlHttp;
////创建异步请求对象
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else if(window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
}

//显示异步查询结果
function showResult(sText){
var oSpan = document.getElementById("UserResult");
oSpan.innerHTML = sText;
if(sText.indexOf("already exists") >= 0)
//如果用户名已被占用
oSpan.style.color = "red";
else
oSpan.style.color = "black";
}

function startCheck(oInput){
//首先判断是否有输入,没有输入直接返回,并提示
if(!oInput.value){
oInput.focus(); //聚焦到用户名的输入框
document.getElementById("UserResult").innerHTML = "User cannot be empty.";
return;
}
//创建异步请求
createXMLHttpRequest();
var sUrl = "9-9.aspx?user=" + oInput.value.toLowerCase() + "×tamp=" + new Date().getTime();
xmlHttp.open("GET",sUrl,true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
showResult(xmlHttp.responseText); //显示服务器结果
}
xmlHttp.send(null);
}
</script>
</head>
<body>
<form name="register">
<!-- 利用文本框的onblur()函数,即当光标离开该文本框时调用相关的函数-->
用户名:<input type="text" onblur="startCheck(this)" name="User"><span id="UserResult"></span><br />
输入密码:<input type="password" name="passwd1"/><br />
确认密码:<input type="password" name="passwd2"/><br />
<input type="submit" value="注册"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>



服务器端处理文件9-9.aspx,通常服务器接收到用户的输入后会查询数据库,检测该用户是否已经被注册,然后将查询结果返回给客户端。

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
<%@ Import Namespace="System.Data" %>
<%
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma","no-cache");

//获得用户输入如果是isaac则代表已注册,把查询结果返回给客户端
if(Request["user"]=="isaac")
Response.Write("Sorry, " + Request["user"] + " already exists.");
else
Response.Write(Request["user"]+" is ok.");
%>



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: