搜索
javascript - ajax做表单验证,还没有学习iquery,hibernate等框架,如何实现只用一个servlet处理这么多的ajax请求
PHPz
PHPz 2017-04-10 16:32:11
[JavaScript讨论组]

先贴servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mall.dao.UserDao;
import mall.model.User;
import mall.tool.DaoFactory;
import mall.tool.JDBC;

public class UserRegister extends HttpServlet {

    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        String userId=request.getParameter("userId");
        String password=request.getParameter("password");
        String mobilePhone=request.getParameter("mobilePhone");
        String email=request.getParameter("email");
        
        Connection conn=JDBC.getCurrentConnection();
        UserDao dao=DaoFactory.getUserDao();

        User u=new User();
        
        String action= request.getParameter("action");
        System.out.println("1");
        System.out.println(action);
        System.out.println("2");
        System.out.println(userId);
        String yes="true";
        String no="false";
        
        try{
            if(null==userId||"".equals(userId)){
                out.write("用户名不能为空");    
            }else if(action.equals("a")&&dao.findById(userId)){
                out.write("用户名已存在!请更改");    
            }else{
                out.write("用户名可用");    
            }        
        } catch (Exception e) {
                e.printStackTrace();
        }    
        
        /////////////////
        if(null==password||"".equals(password)){
            out.write("密码不能为空");        
        }else if(password.length()<8){
            out.write("密码长度至少为8位");
        }else{
            out.write("密码格式正确");
        }
        out.flush();
        out.close();
    }

    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("post");
        doGet(request, response);
        
    }

}

以下是js

<script>
 function checkUserId(){
      var userId=document.getElementById("userId").value;
      var check=new XMLHttpRequest();
      check.onreadystatechange=function(){
          if(check.readyState==4 &&check.status==200){
              document.getElementById("checkout").innerHTML=check.responseText;
          }
      }
  
      check.open("POST","<%=path%>/UserRegister",true);
      check.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      
      check.send("userId="+userId+"&action=a");
  }
  
  function checkPw(){
      var password=document.getElementById("password").value;
      var check=new XMLHttpRequest();
      check.onreadystatechange=function(){
          if(check.readyState==4 &&check.status==200){
              document.getElementById("checkPassword").innerHTML=check.responseText;
          }
      }
      check.open("POST","<%=path%>/UserRegister",true);
      check.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      check.send("password"+password+"&action=a");
  }
</script>

以下是我的表单项

 <tr>
      <td width="40%" class="tablebody1"><b>用户名</b>:<br>
        注册用户名长度限制为0-16字节</td>
      <td width="60%" class="tablebody1">
        <input type="text" name="userId" id="userId" onchange="checkUserId()" maxlength="16" size="32" value="" style="font-family: Tahoma,Verdana,宋体; font-size: 12px; line-height: 15px; color: #000000">
        <font color="#FF0000">*</font>
        <!-- 这里用来显示用户名是否可用-->
        <p id="checkout" > </p>
      </td>
    </tr>
    <tr>
      <td width="40%" class="tablebody1"><b>密码(至少6位,至多8位)</b>:<br>
    请输入密码,区分大小写。<br>请不要使用任何类似 \'*\'、\' \' 或 HTML 字符'</td>
      <td width="60%" class="tablebody1">
    <input type="password" name="password" id="password" maxlength="8" size="32" value="" style="font-family: Tahoma,Verdana,宋体; font-size: 12px; line-height: 15px; color: #000000">
    <font color="#FF0000">*</font>
    <p id="checkPassword"> </p></td>
    </tr>

结果是这样的

求达人助一臂之力,新手

PHPz
PHPz

学习是最好的投资!

全部回复(2)
迷茫

加个标记进行区分就好了
不同的验证使用不同的标记

PHP中文网

多说一句,登陆就把get请求禁掉吧,password都明文传输了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号