javaweb基礎----使用ajax校驗用戶名是否重復


首先是前端js的代碼:

 1  <script type="text/javascript">
 2         function checkForm(){
 3             //1.驗證用戶名不能為空
 4             var userName = document.getElementById("userName").value;
 5             if(userName==""){
 6                  var tip1 = document.getElementById("tip1");
 7                 tip1.innerHTML = "用戶名不能為空!";
 8                 //alert("用戶名不能為空!")
 9                 return false;
10             };
11             //2.驗證昵稱不能為空
12             var nickName = document.getElementById("nickName").value;
13             if(nickName==""){
14                  var tip2 = document.getElementById("tip2");
15                 tip2.innerHTML = "昵稱不能為空!";
16                 //alert("用戶名不能為空!")
17                 return false;
18             };
19               //3.驗證密碼不能為空
20             var pwd = document.getElementById("pwd").value;
21             if(pwd==""){
22                  var tip3 = document.getElementById("tip3");
23                 tip3.innerHTML = "密碼不能為空!";
24                 //alert("用戶名不能為空!")
25                 return false;
26             };
27             //4.驗證兩次密碼是否一致
28             var repwd = document.getElementById("repwd").value;
29             if(pwd!=repwd){
30                  var tip4 = document.getElementById("tip4");
31                 tip4.innerHTML = "兩次密碼不一致!";
32                 //alert("用戶名不能為空!")
33                 return false;
34             };
35             //5.驗證驗證碼不能為空
36             var code = document.getElementById("code").value;
37             if(code==""){
38                  var tip5 = document.getElementById("tip5");
39                 tip5.innerHTML = "驗證碼不能為空!";
40                 //alert("用戶名不能為空!")
41                 return false;
42             };
43             //6.修改全局變量exist的值
44             if(exist){
45                 return false;
46             }
47             return true;
48         }
49         //7.后端數據傳回的校驗信息
50         if("${msg }"){
51             alert("${msg }")
52         }
53         //定義全局變量exist,用於表示用戶名是否存在,默認不存在
54         var exist = false;
55         // 獲取XMLHttpRequest對象
56         function getXMLHttpRequest() {
57             var xmlhttp;
58             // code for IE7+, Firefox, Chrome, Opera, Safari
59             if (window.XMLHttpRequest) {
60                 xmlhttp = new XMLHttpRequest();
61             }
62             // code for IE6, IE5
63             else {
64                 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
65             }
66             return xmlhttp;
67         }
68             //7.利用ajax檢測用戶名是否重復
69             function  checkName(username) {
70                 if(username){
71                     //1.獲取XmlHttpRequest對象
72                     var xhr = getXMLHttpRequest();
73                     //2.監聽服務器返回狀態
74                     xhr.onreadystatechange = function(){
75                         if(xhr.readyState==4&&xhr.status==200){
76                             //3.獲取后台返回的結果
77                             var data = xhr.responseText;
78                             //判斷返回信息
79                             if(data=="1"){
80                                 //data=="1",用戶名可用,表單可提交
81                                 document.getElementById("tip12").innerHTML="用戶名可用";
82                                 exist = false;
83                             }else{
84                                 //data==其它,用戶名不可用,表單不可提交
85                                 document.getElementById("tip11").innerHTML="用戶名不可用";
86                                 exist = true;
87                             }
88                         }
89                     }
90                     //4.發送請求
91                     xhr.open("POST","${root }/checkNameServlet",true);
92                     // 使用表單的方式 POST 數據
93                     xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
94                     xhr.send("username="+username);
95                 }
96             }
97     </script>

然后是表單中的form設置:

1 <form action="${root }/registerServlet" method="post" onsubmit="return checkForm();" id="registForm">
2             <dl class="register_row">
3                 <dt>用戶名:</dt>
4                 <dd><input id="userName" name="username" class="register_input" onblur="checkName(value)"/><font id="tip11" color="red"></font><font id="tip12"color="green"></font></dd>
5             </dl>

說明:重點看43-46行,53行,79-86行。

后端代碼:

1 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2         String username = request.getParameter("username");
3         UserService us =new  UserServiceImpl();
4         String data = us.check(username);
5         response.getWriter().write(data);
6     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM