ajax验证编号是否重复 - [项目相关]

来源:互联网 发布:linux查看oracle实例名 编辑:程序博客网 时间:2024/05/23 13:12

//js文件中的写法

var XMLHttpReq;
function enoCheck()
{
    var eno=document.getElementById('eno').value;
   
    var url="/hr/empValid.do?eno="+eno;
    if(window.XMLHttpRequest)
    XMLHttpReq=new XMLHttpRequest();
    else if(window.ActiveXObject)
    XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP");
   
    if(XMLHttpReq)
     {
     XMLHttpReq.open("GET",url,true);
     XMLHttpReq.onreadystatechange=processResponse;
     XMLHttpReq.send(null);
     }
}

function processResponse()
{
if(XMLHttpReq.readyState==4)
    {
         if(XMLHttpReq.status==200)
         {
         var xmlDoc=XMLHttpReq.responseXML.documentElement;
         var node=xmlDoc.getElementsByTagName('info');
         var validText=node[0].firstChild.nodeValue;
         if(validText=="员工号可用.")
             document.getElementById('check_no').innerHTML="<font color=\"green\">"+validText+"</font>";
         else
             document.getElementById('check_no').innerHTML=validText;
         }
         else
         {
         alert("Not able to retrieve description "+XMLHttpReq.statusText);
         }
    }
    else
    document.getElementById('check_no').innerHTML="正在验证员工号……";
}

//Action中验证方法

    // 验证员工编号
    public ActionForward valid(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("text/xml");
        response.setCharacterEncoding("GBK");
        response.setHeader("Cache-Control", "no-cache");

        String eno = (String) request.getParameter("eno");
        String xml = "<?xml version=\"1.0\" encoding=\"gbk\" ?>";
        if (eno.equals("") || eno == null) {
            xml += "<message><info>员工号不能为空!</info></message>";
        } else if (isExist(eno, request)) {
            xml += "<message><info>员工号已存在,请重新填写!</info></message>";
        } else {
            xml += "<message><info>员工号可用.</info></message>";
        }
        response.getWriter().write(xml);
        return null;
    }

    /**
     * 判断员工号是否存在
     *
     * @param eno
     *            员工号
     * @return boolean 标识员工是否存在
     */

    private boolean isExist(String eno, HttpServletRequest request) {
        // 声明一个员工服务实例
        EmployeeService es = EmployeeServiceImpl.getInstance();
        LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
        ArrayList<Employee> employees = es.findEmployees(map);// 查询所有员工
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("currentEmployee");
        if (employee != null && employee.getEno().equals(eno))
            return false;
        // 如果存在相同编号返回true,否则返回false
        for (int i = 0; i < employees.size(); i++) {
            if (employees.get(i).getEno().equals(eno))
                return true;
            else
                continue;
        }
        return false;
    }

3//在jsp页面中添加一个id为check_no的<span>标签即可,并在onblur事件中调用js中的check_eno()方法

原创粉丝点击