jquery bootstrapValidator中的远程验证器

时间:2014-04-29 08:51:46

标签: jquery jquery-validate twitter-bootstrap-3

远程验证无效。它始终显示为用户名无效。我是这个jquery验证的新手。那么任何人都可以告诉我们错误是什么。

username: {
                        message: 'The username is not valid',
                        validators: {
                            notEmpty: {
                                message: 'The username is required and cannot be empty'
                            },
                            stringLength: {
                                min: 6,
                                max: 30,
                                message: 'The username must be more than 6 and less than 30 characters long'
                            },
                            regexp: {
                                regexp: /^[a-zA-Z0-9_\.]+$/,
                                message: 'The username can only consist of alphabetical, number, dot and underscore'
                            },
                            remote: {
                                url: 'remote.jsp',
                                type: "post",
                                async: false,
                                data:
                                        {
                                            username: function(validator)
                                            {
                                                return $('#defaultForm :input[name="username"]').val();

                                            }
                                        },
                                message: 'The username is not available',
                            },

在后端,我正在使用JSP,代码是:

    ResultSet rs;
    Boolean flag = true;

    String username = request.getParameter("username");
    rs = st.executeQuery("select * from members where user_name = '" + username + "'");
    if (rs.next()) {
        flag = false;
        out.print("false");    //already exist  

    } else {
        flag = true;
        out.print("true");   // not exist 
    }

3 个答案:

答案 0 :(得分:0)

使用servlet

username: {
            message: 'The username is not valid',
            validators: {
                notEmpty: {},
                stringLength: {
                    min: 6,
                    max: 20
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9_\.]+$/
                },

                remote: {
                url: './CheckUsernameServlet',
                type: 'POST',
                delay: 2000  

                }

            }
        },

在servlet代码中

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    JSONObject json = new JSONObject();
    PrintWriter out=response.getWriter();
    response.setContentType("application/json");
    String email=request.getParameter("username");
    if(userDAO.validateEmail(email)){

        try {
            json.put("valid",true);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else {

        try {
            json.put("valid",false);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    out.print(json);
}

答案 1 :(得分:0)

更改代码 写上面的代码

 username: {
      message: 'The username is not valid',
      validators: {
                notEmpty: {},
                stringLength: {
                min: 6,
                max: 20
            },
                regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/
            },

                remote: {
                url: './CheckUsernameServlet',
                type: 'POST',
                delay: 2000                
             }
         }
  },

<%@ page import="java.sql.*" %>
<%@ page import="org.json.JSONException" %> 
<%@ page import="org.json.JSONObject" %>  
<% 
    ResultSet rs=null;
    Boolean flag = true;
    Statement st=null;
    JSONObject json = new JSONObject();
    String username = request.getParameter("username");
    rs = st.executeQuery("select * from members where user_name = '" + username + "'");
    if (rs.next()){
        flag = false;
        flag = false;
        json.put("valid",false); 
    } 
    else {
        flag = true;
        json.put("valid",true);
    }
    out.print(json);
%>

答案 2 :(得分:0)

1年后回答

我所做的只是将返回的json更改为有效的Json数据.Cuz上面的答案是正确的,但它没有返回有效的Json数据。

    public class RegistrationEmailCheck extends HttpServlet {
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");

  PrintWriter out = response.getWriter();
JSONObject json = new JSONObject();
String availEmail = request.getParameter("email");
System.out.println(availEmail);
String SQL = "SELECT email FROM login WHERE email='" + availEmail + "'";
Connection con = DBInfo.getConn();
Statement st;
ResultSet rs;

try {
    st = con.createStatement();
    rs = st.executeQuery(SQL);


    if (rs.next()) {

        out.print("{\"valid\" : false }");
        json.put("valid", false);
        System.out.println("false");


    } else {

        out.print("{\"valid\" : true }");
        json.put("valid", true);
        System.out.println("true");


    }


} catch (Exception ex) {
    ex.printStackTrace();
} finally {

    out.close();
     }



   }


 }

现在一切正常。