注册 登录
编程论坛 jQuery论坛

求一个ajax做数据验证的实例

yoyodo 发布于 2006-08-11 22:52, 1679 次点击

大概要mvc模型吧,用javabean做数据库操作
还有Login.java 和DataBean.java的代码不会.
谁能给个猜考一下

以下是login.htm的代码:
<!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=gb2312" />
<title>首页</title>
<script type="text/javascript">
var xmlHttp;

//创建httpRequest请求
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function userNameCheck() {
var userName = document.getElementById("userName").value;

if(userName == "") {
window.alert("用户名不能为空.");
document.form1.userName.focus();
return false;
}
else {
var url = "Login?userName=" + userName + "&ts=" + new Date().getTime();
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleAddStateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
}

function handleAddStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200 || xmlHttp.status == 0) {
var mes = xmlHttp.responseXML.getElementsByTagName("message")[0].firstChild.data;
var val = xmlHttp.responseXML.getElementsByTagName("passed")[0].firstChild.data;
setMessage(mes, val);
}
else {
alert(xmlHttp.status);
alert("Error while adding employee.");
}
}
}

function setMessage(message, isValid) {
var messageArea = document.getElementById("showMessage");
var fontColor = "red";

if (isValid == "true") {
fontColor = "green";
}
messageArea.innerHTML = "<font color=" + fontColor + ">" + message + " </font>";
}
</script>
</head>

<body>
<center>
<br /><br />
<table>
<form action="#" name="form1">
用户名: <input type="text" size="15" name="userName" onblur="userNameCheck();" id="userName"/>
&nbsp;&nbsp;<a href="reg?action=reg">注册</a><br />
密&nbsp;&nbsp;码:<input type="password" size="15" name="usePassword" id="password"/><br />
<input type="button" value="登录" name="submit" />
</form>
<div id="showMessage"></div>
</table>
</center>
</body>
</html>

2 回复
#2
ming2062006-08-15 17:01
var http_request=false;
//利用XMLHttpRequest发送请求
function send_request(url,nn){
http_request=false;
//判断浏览器类别
if(window.XMLHttpRequest){
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType("text/xml");
}
}else if(window.ActiveXObject){
try{
http_request=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){}
}
//异常,创建对象失败
if(!http_request){
window.alert("不能创建XMLHttpRequest对象实际!");
return false;
}
//
http_request.onreadystatechange=function(){proRequest(nn)};
//确定发送请求的方式和RUL以及是否同步执行以下代码
http_request.open("GET",url,true);
http_request.send(null);
}
//处理返回信息的函数
function proRequest(n){
if(http_request.readyState==4){//判断对象状态
if(http_request.status==200){//信息已经成功返回,并开始处理信息
//来自不同的请求
if(n==1){
var str=http_request.responseText;
alert(str);
if(str!="恭喜,该用户可以注册!"){
//若不是得到该提示信息,一律要求文本获得焦点
document.form1.username.select();
document.form1.username.focus();
}
//处理另外一个返回信息
}else if(n==2){
document.getElementById(currentPos).innerHTML=http_request.responseText;
}
}else{
alert("您所请求的页面有异常!");
}
}
}
//表单检测并提交
function userCheck(){
var f=document.form1;
var username=f.username.value;
if(username==""){
window.alert("用户名不可为空,请输入用户名!");
f.username.focus();
return false;
}else
send_request("index.aspx?username="+username,1);
}


我想,服务断无论是用JSP,ASP.ENT,ASP都是无所喂的.
#3
ming2062006-08-15 17:03

引用部分,贴与不贴一个样.
<DIV class="cc" align="center">
<form id="form1" name="form1" method="post" action="index.aspx">
<div id="div_utitle">用户名:
<label>
<input type="text" name="username"/>
</label>
</div>

<div id="div_check">
<label>
<input type="button" name="Submit" value="检测是否可用" onClick="userCheck();" />
</label>
</div>

<div id="div_submit">
<label>
<input type="submit" name="Submit" value="提交数据"/>
</label>
</div>
</form>
</DIV>

1