ajax驗證使用者名稱是否存在

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   sp   java   

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(data=="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驗證使用者名稱是否存在

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.