ajax验证注册表单-(2页面) 本实例保存即可使用
ajax和提交页 index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Ajax验证表单实例</title> <script type="text/javascript"> //----------------------------------------------------------------------- //打开服务器响应过程函数 //----------------------------------------------------------------------- function callback(xmlrequest){ if(xmlrequest.readyState==4){ var result=xmlrequest.responseText; document.getElementById("checkInfo").innerHTML=result; } } //----------------------------------------------------------------------- //判断浏览器是否支持AJAX函数---通用函数 //------------------------------------------------------------------------ function getRequest(){ var request; //定义声明新变量request // 根据浏览器插件判断是否非IE浏览器 如果是非IE 则 if(window.XMLHttpRequest) { request = new XMLHttpRequest(); if (request.overrideMimeType) { request.overrideMimeType("text/xml"); } } // 根据浏览器插件判断是否是IE浏览器 如果是IE 则 else if (window.ActiveXObject) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } return request; } </script> <script type="text/javascript"> //----------------------------------------------------------------------- //服务器响应过程函数go() //------------------------------------------------------------------------ var request; function go(){ //判断表单输入内容 var form=document.loginForm; //form.onsubmit=function(){ if(form.username.value==""){ document.getElementById("checkInfo").innerHTML="用户名不能为空"; return; } if(form.pass.value==""){ document.getElementById("checkInfo").innerHTML="密码不能为空"; return; } //打开浏览器判断函数 request=getRequest(); if (request==null){ alert ("您的浏览器不支持AJAX!"); return; } if(request){ //构造发送数据字符串 var url="ajax.asp"; var formData="username="+form.username.value+"&pass="+form.pass.value; url= url+ "?"+formData; //alert (url); request.open("GET",url,true); request.onreadystatechange=function(){ callback(request); } request.send(null); } } </script> </head> <body> <div id="checkInfo">请填写用户名和密码</div> <!-- <form action="ajax.asp" method="post" name="loginForm" >--> <form action="" method="post" name="loginForm" > <input type="text" name="username" /> <br /> <input type="password" name="pass" /> <br /> <!-- <input type="submit" name="submit" value="提交验证" /> --> <input type="button" name="Submit" value="提交验证" > <br /> </form> </body> </html>
动态处理页 ajax.asp
<% response.expires=-1 dim user,password user="admin" password="admin" if request("username")<>user or request("pass")<>password then response.Write("用户名或密码错误") else response.Write("恭喜你,验证通过") end if %>
--------------------------------------------------------------------------------
演示当输入密码正确并且不为空 就提示验证通过,否则提示拥护名密码出错
按字母顺序浏览:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
→我们致力于为广大网民解决所遇到的各种电脑技术问题 如果您认为本词条还有待完善,请 编辑词条