1. 程式人生 > >JAVA-----Ajax實現註冊使用者名稱唯一性檢查

JAVA-----Ajax實現註冊使用者名稱唯一性檢查

通過對HTML輸入完畢後,釋放編輯框焦點。執行判斷是否使用者名稱已被註冊的及時提醒操作。

html文字

<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>Ajax實現使用者註冊名唯一性檢查</title>
<meta http-equiv="content-type" content="text/html; charset=GB2312">
<style type="text/css">
<!--
.style1 {font-size: 12px}
.style2 {color: #FF0000}
-->
</style>
</head>
<script language="javascript">
var xmlHttpReq=null;
function createRequest(){
if(window.XMLHttpRequest){
xmlHttpReq=new XMLHttpRequest();
}else if(window.ActiveXObject){
xmlHttpReq=new ActiveXOject("Microsft.XMLHTTP");
}
 
}
function getBackInfo(){
createRequest();
var username=document.getElementById("username").value;
if(username.trim().length==0){
document.getElementById("disCheckResult").innerHTML="user no empyt!";
document.getElementById("username").focus();
return false;
}
if(xmlHttpReq!=null){
var url="checkUser.jsp?username="+username;
xmlHttpReq.open("get",url,true);
xmlHttpReq.onreadystatechange=disResult;
xmlHttpReq.send(null);
}else{
alert(":nonononono!!!!");
 
}
 
}
function disResult(){
if(xmlHttpReq.readyState==4){
if(xmlHttpReq.status==200){
alert(xmlHttpReq.responseText);
document.getElementById("disCheckResult").innerHTML=xmlHttpReq.responseText;
 
}else{
alert("eorr!");
}
}
}
</script>
<body>
<form name="form1" method="post" >
<table width="100%" border="0" align="center" bgcolor="#0099FF">
<tr>
<th colspan="3" scope="col"> 使用者註冊</th>
</tr>
<tr bgcolor="#FFFFFF">
<th width="36%" rowspan="7" scope="row"></th>
<th width="18%" height="46" scope="row"><div align="left">
<span style="font-weight: 400"><font size="2">使用者名稱:</font></span></div></th>
<td width="46%"><input name="username" type="text" onblur="getBackInfo()" id="username" >
<span class="style2">*</span><span id=disCheckResult style="color:red"> </span> </td>
</tr>
<tr bgcolor="#FFFFFF">
<th height="39" scope="row"><div align="left">
<span style="font-weight: 400"><font size="2">密碼:</font></span></div></th>
<td><input name="pwd" type="password" id="pwd">
<span class="style2">*</span></td>
</tr>
<tr bgcolor="#FFFFFF">
<th height="39" scope="row"><div align="left">
<span style="font-weight: 400"><font size="2">確認密碼:</font></span></div></th>
<td><input type="password" name="pwd1">
<span class="style2">*</span></td>
</tr>
<tr bgcolor="#FFFFFF">
<th height="39" scope="row"><div align="left">
<span style="font-weight: 400"><font size="2">真實姓名:</font></span></div></th>
<td><input name="header" type="text">
<span class="style2">*</span></td>
</tr>
<tr bgcolor="#FFFFFF">
<th height="39" scope="row"><div align="left">
<span style="font-weight: 400"><font size="2">電話:</font></span></div></th>
<td><input name="phone" type="text">
<span class="style2">*</span></td>
</tr>
<tr bgcolor="#FFFFFF">
<th height="39" scope="row"><div align="left">
<span style="font-weight: 400"><font size="2">電子郵箱:</font></span></div></th>
<td><input name="email" type="text">
<span class="style2">*</span></td>
</tr>
<tr bgcolor="#FFFFFF">
<th height="37" scope="row"> </th>
<td><input type="submit" name="Submit" value="提交">
<input type="reset" name="Reset" value="重填"></td>
</tr>
</table>
</form>
</body>
</html>


java的程式碼

<%@ page language="java" import="java.util.*,java.sql.*,ajck.aj.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<%
String username=request.getParameter("username");
String sql="select * from users where username=’"+username+"’";
ConnDB conn=new ConnDB();
ResultSet rs= conn.doQuery(sql);
if(rs.next()){
out.println("賬號已被註冊");
}else{
out.println("可以註冊");
}
 
%>