ajax檢測用戶名(email格式的用戶名)
<input name=email class=clear_input id=email maxlength="50" value="<%=email%>" onBlur="javascript:checkEnglish()"/>
<span id=reg_email_error style="DISPLAY: none">ReadyState</span>
function checkEnglish(){
if (!CheckIfEnglish(document.insertForm.email.value )) {
alert("請輸入數字,字母和下劃線!");
document.insertForm.email.value="";
document.insertForm.email.focus();
return false;
}
else {
doCheck();
return true;
}
}
function CheckIfEnglish( String ){
var Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-_";
var i;
var c;
if(String.charAt( 0 )=='-')
return false;
if( String.charAt( String.length - 1 ) == '-' )
return false;
for( i = 0; i < String.length; i ++ ){
c = String.charAt( i );
if (Letters.indexOf( c ) < 0)
return false;
}
return true;
}
ajax.js
//定義XMLHttpRequest對象實例
var http_request = false;
//定義可復用的http請求發送函數
function send_request(method,url,content,responseType,callback) {//初始化、指定處理函數、發送請求的函數
http_request = false;
//開始初始化XMLHttpRequest對象
if(window.XMLHttpRequest) { //Mozilla 瀏覽器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//設置MiME類別
http_request.overrideMimeType("text/xml");
}
}
else if (window.ActiveXObject) { // IE瀏覽器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 異常,創建對象實例失敗
window.alert("不能創建XMLHttpRequest對象實例.");
return false;
}
if(responseType.toLowerCase()=="text") {
//http_request.onreadystatechange = processTextResponse;
http_request.onreadystatechange = callback;
}
else if(responseType.toLowerCase()=="xml") {
//http_request.onreadystatechange = processXMLResponse;
http_request.onreadystatechange = callback;
}
else {
window.alert("響應類別參數錯誤。");
return false;
}
// 確定發送請求的方式和URL以及是否異步執行下段代碼
if(method.toLowerCase()=="get") {
http_request.open(method, url, true);
}
else if(method.toLowerCase()=="post") {
http_request.open(method, url, true);
http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
}
else {
window.alert("http請求類別參數錯誤。");
return false;
}
http_request.send(content);
}
// 處理返回文本格式信息的函數
function processTextResponse() {
if (http_request.readyState == 4) { // 判斷對象狀態
if (http_request.status == 200) { // 信息已經成功返回,開始處理信息
//alert(http_request.responseText);
alert("Text文檔響應。");
} else { //頁面不正常
alert("您所請求的頁面有異常。");
}
}
}
//處理返回的XML格式文檔的函數
function processXMLResponse() {
if (http_request.readyState == 4) { // 判斷對象狀態
if (http_request.status == 200) { // 信息已經成功返回,開始處理信息
//alert(http_request.responseXML);
alert("XML文檔響應。");
} else { //頁面不正常
alert("您所請求的頁面有異常。");
}
}
}
function doCheck() {
var f = document.forms[0];
if(f.email.value!="") {
document.getElementById("reg_email_error").innerHTML = "系統正在處理您的請求,請稍后。";
send_request("GET","checkUsername.jsp?email="+f.email.value,null,"text",showFeedbackInfo);
}
else {
document.getElementById("reg_email_error").innerHTML = "請輸入用戶名稱。";
}
}
checkUsername.jsp
<%@ page contentType="text/html; charset=gbk"%>
<%@ page import="com.beyoung.blog.hql.admin.huiyuangguanli.HuiyuangguanliHql" %>
<%@ page import="com.beyoung.blog.bean.Huiyuangguanli" %>
<%@ page import="com.beyoung.blog.util.strDate.PubFun" %>
<%
String name=request.getParameter("email");
name=PubFun.toGBK(name);
System.out.println(name);
System.out.println(" :"+HuiyuangguanliHql.getHuiyuangguanliHql().getHuiyuangguanliKey(name));
if(!HuiyuangguanliHql.getHuiyuangguanliHql().getHuiyuangguanliKey(name)){
out.println("<font color='#FF0000'>用戶名稱["+name+"]已占用!</font>");
}
else{
out.println("<font color='#088EOF'>用戶名稱["+name+"]尚未被注冊,您可以繼續。</font>");
}
%>
posted on 2008-12-15 11:31 小卓 閱讀(907) 評論(3) 編輯 收藏 所屬分類: html and js