JavaWeb使用Ajax實現仿163郵箱註冊檢查該賬戶是否存在
阿新 • • 發佈:2021-01-22
JavaWeb使用Ajax實現仿163郵箱註冊檢查該賬戶是否存在,如圖所示:根據資料庫顯示,可看到賬戶存在,則會提示賬戶已存在。
首先,是註冊頁面
regist.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="/admin/common.jspf"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<link rel="stylesheet" type="text/css"
href="${basePath}/index/css/regist.css" />
<title>Insert title here</title>
</head>
<body>
<title>jq+css3使用者註冊</title>
<script id="jquery_183" type="text/javascript" class ="library"
src="js/jquery-1.8.3.min.js"></script>
</head>
<body>
<div id="top">
<h1>使用者註冊</h1>
</div>
<div class="form-bak">
<form method="post" action="#" onsubmit="adaptValue();">
<div class="left">
<div class="line" id="line_account">
<div class="info">
<strong>郵箱</strong> <span class="tips">郵箱就是賬號</span>
</div>
<div class="input">
<input type="text" name="account" id="account"
onblur="v_account();" onkeyup="v_account();" />
<div class="none">
<span></span>
</div>
</div>
</div>
<div class="line" id="line_name">
<div class="info">
<strong>使用者名稱</strong> <span class="tips">只能由數字、文字或下劃線組成<br />最多32個字元
</span>
</div>
<div class="input">
<input type="text" name="name" id="name" onblur="v_name();"
onkeyup="v_name();" />
<div class="none">
<span></span>
</div>
</div>
</div>
<div class="line" id="line_password">
<div class="info">
<strong>密碼</strong> <span class="tips">6至16個字元</span>
</div>
<div class="input">
<input type="password" name="password" id="password"
onblur="v_password();" onkeyup="v_password();" />
<div class="none">
<span></span>
</div>
</div>
</div>
<div class="line" id="line_repeat">
<div class="info">
<strong>確認密碼</strong> <span class="tips">再次輸入密碼</span>
</div>
<div class="input">
<input type="password" name="repeat" id="repeat"
onblur="v_repeat();" onkeyup="v_repeat();" />
<div class="none">
<span></span>
</div>
</div>
</div>
</div>
<div class="right">
<input type="submit" id="submit" value="完成註冊" disabled="disabled" />
<div class="readagreement-wrap" onclick="onReadAgreementClick();">
<input type="checkbox" name="agree" id="agree"
onchange="v_submitbutton();" /> 同意《<a id="readagreement"
href="#agreement" onclick="showAgreement();" title="檢視使用者使用協議">使用者使用協議</a>》
</div>
<div>
已經擁有賬號,<a href="#">直接登入</a>
</div>
<div>
忘記密碼,<a href="#">請聯絡管理員</a>
</div>
</div>
</form>
</div>
<div id="agreement" class="agreement" style="display: none;">
<h1>使用者使用協議</h1>
<iframe src=""></iframe>
<input type="button" value="同意" onclick="agree();" /> <a href="#"
class="backtotop">返回頂部</a>
</div>
</body>
</html>
<script>
function enableSubmit(bool) {
if (bool)
$("#submit").removeAttr("disabled");
else
$("#submit").attr("disabled", "disabled");
}
function v_submitbutton() {
if ($("#agree").attr("checked") != "checked") {
enableSubmit(false);
$(".readagreement-wrap").css("outline", "1px solid #f99");
return;
} else {
$(".readagreement-wrap").css("outline", "1px solid #9f9");
}
for (f in flags)
if (!flags[f]) {
enableSubmit(false);
return;
}
enableSubmit(true);
}
function showAgreement() {
$("#readagreement").removeAttr("onclick");
$("#agreement").show();
$("#agreement iframe").attr("src", "http://www.baidu.com");//此處為連結地址
}
function agree() {
$("#agreement").hide();
$("#readagreement").attr("onclick", "showAgreement();");
if ($("#agree").attr("checked") != "checked")
$("#agree").trigger("click");
}
function onReadAgreementClick() {
return;
if ($("#agree").attr("checked")) {
$("#agree").removeAttr("checked");
} else {
$("#agree").attr("checked", "checked");
}
v_submitbutton();
}
var flags = [ false, false, false, false ];
//郵箱驗證,網上找到的正則
var RegEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9][email protected]([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
function lineState(name, state, msg) {
if (state == "none") {
$("#line_" + name + " .input div").attr("class", "none");
return;
}
if (state == "corect") {
$("#line_" + name + " .input div").attr("class", "corect");
return;
}
$("#line_" + name + " .input span").text(msg);
$("#line_" + name + " .input div").attr("class", "error");
}
function v_account() {
var account = $("#account").val();
if (!RegEmail.test(account)) {
lineState("account", "error", "郵箱格式不正確");
flags[0] = false;
enableSubmit(false);
}
else {
lineState("account", "corect", "");
flags[0] = true;
}
v_submitbutton();
}
function v_name() {
var name = $("#name").val();
(function queryList(){
//定義 傳送的引數
var parm="action=checkname&&name="+name;
//傳送ajax請求
$.ajax({
dataType:"json",
type:"post",
url:"${basePath}/UserServlet?"+parm,
success:function(data){
//呼叫填充資料的函式
if(data==null){
}else{
//alert("錯誤");
lineState("name", "error", "當前使用者已存在");
}
},
error:function(){
alert("請求失敗");
}
});
})()
if (name.length == 0) {
lineState("name", "error", "不得為空");
flags[1] = false;
}
else {
if (name.length > 32) {
lineState("name", "error", "必須少於32個字元");
flags[1] = false;
}
else {
lineState("name", "corect", "");
flags[1] = true;
}
}
v_submitbutton();
}
function v_password() {
var password = $("#password").val();
if (password.length < 6) {
lineState("password", "error", "必須多於或等於6個字元");
flags[2] = false;
}
else {
if (password.length > 16) {
lineState("password", "error", "必須少於或等於16個字元");
flags[2] = false;
}
else {
lineState("password", "corect", "");
flags[2] = true;
}
}
v_repeat();
v_submitbutton();
}
function v_repeat() {
if (!flags[2]) {
lineState("repeat", "none", "");
return;
}
if ($("#password").val() != $("#repeat").val()) {
lineState("repeat", "error", "密碼不一致");
flags[3] = false;
}
else {
lineState("repeat", "corect", "");
flags[3] = true;
}
v_submitbutton();
}
function adaptValue() {
return true;
}
</script>
</body>
</html>
UserServlet.java
private void checkname(HttpServletRequest request,
HttpServletResponse response) throws IOException {
// TODO Auto-generated method stub
PrintWriter out=response.getWriter();
String name=request.getParameter("name");
User user=userservice.checkname(name);
String json=JSON.toJSONString(user, true);
System.out.println(json);
out.write(json);
out.flush();
out.close();
}
UserDaoImpl.java
public Book findByid(int id) {
// TODO Auto-generated method stub
String sql="select books.id id,books.name name,info,types.name typename,bookDate from books inner join types on types.id=books.typeid where books.id=?";
Object[] obj={id};
Book book=null;
try {
book=(Book) dbutil.getObject(Book.class, sql, obj);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return book;
}
按照這樣就可以實現以上頁面了,碼字不易,給個贊吧!