首页 > 代码库 > ajax验证用户名是否存在

ajax验证用户名是否存在

js代码

function validatorloginName(){      var loginName=document.getElementById("suser").value;     var reg=new RegExp("^[a-zA-Z]{1}[a-zA-Z0-9]{5,11}$");    if(loginName == "")      {  $("input[name=‘sname‘]").next().removeClass("ok");       $("input[name=‘sname‘]").next().html("用户名不能为空!");       $("input[name=‘sname‘]").next().addClass("err");         return;      }      else if(!reg.exec(loginName)){        $("input[name=‘sname‘]").next().removeClass("ok");        $("input[name=‘sname‘]").next().html("用户名格式错误,首位必须是字母长度不能小于六位!");        $("input[name=‘sname‘]").next().addClass("err");        return false;    }else{        $("input[name=‘sname‘]").next().html("");         $.ajax({                 type: "POST",                      url: "../servlet/ValidateName",                       data: "loginName="+loginName,                   success: function(data){                 if(datahttp://www.mamicode.com/=="true"){                    $("input[name=‘sname‘]").next().html("");                   $("input[name=‘sname‘]").next().addClass("ok");                   return true;                               }else{                    $("input[name=‘sname‘]").next().removeClass("ok");                   $("input[name=‘sname‘]").next().html("抱歉!用户名已存在!");                   $("input[name=‘sname‘]").next().addClass("err");                   return false;               }                  }                           });                 }}

web.xml配置

  <servlet>  <servlet-name>ValidateName</servlet-name>    <servlet-class>Util.ValidateName</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>ValidateName</servlet-name>    <url-pattern>/servlet/ValidateName</url-pattern>  </servlet-mapping>

java代码

package Util;import java.io.IOException;import java.io.PrintWriter;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;public class ValidateName extends HttpServlet {    public ValidateName(){        super();    }    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        response.setContentType("text/html");        String loginName=request.getParameter("loginName").toString();            try {            Context ctx = new InitialContext();            DataSource    ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/oracle");            Connection conn = ds.getConnection();            PreparedStatement pst = conn.prepareStatement("select * from huiyuan where suser=‘"+loginName+"‘ ");            ResultSet res=pst.executeQuery();            boolean flog=res.next();            if(!flog){            response.getWriter().write("true");//此值jquery可以接收到              }        } catch (NamingException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }                }        public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        this.doGet(request, response);             }}

 

ajax验证用户名是否存在