日期:2014-05-16  浏览次数:20541 次

ajax验证用户名
var xmlHttp;
	 
	function createXMLHttpRequest() {
		//表示当前浏览器不是ie,如ns,firefox
		if(window.XMLHttpRequest) {
			xmlHttp = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	function validate(field) {
//alert(document.getElementById("userId").value);
		if (trim(field.value).length != 0) {
			//创建XMLHttpRequest
			createXMLHttpRequest();
			var url = "user_validate.jsp?userId=" + trim(field.value) + "&timestamp=" + new Date().getTime();
			xmlHttp.open("GET", url, true);
			//方法地址,处理完成后自动调用,回调
			xmlHttp.onreadystatechange=callback;
			//将参数发送到Ajax引擎
			xmlHttp.send(null);
		}else {
			document.getElementById("userIdSpan").innerHTML = "";
		}
	}
	
	function callback() {
		if(xmlHttp.readyState == 4) { //Ajax引擎初始化成功
			if (xmlHttp.status == 200) { //http协议成功
				document.getElementById("userIdSpan").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>";
			}else {
				alert("请求失败,错误码=" + xmlHttp.status);
			}
		}
	}

<input name="userId" type="text" class="text1" id="userId" size="10" maxlength="10" onkeypress="userIdOnKeyPress()" value="<%=userId %>" onblur="validate(this)"><span id="userIdSpan"></span>


user_validate.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="com.bjpowernode.drp.sysmgr.manager.*" %>    
<%	
	String userId = request.getParameter("userId");
	if (UserManager.getInstance().findUserById(userId) != null) {
		out.println("用户代码已经存在");
	}
%>