使用Jsp Servlet中的bootstrapValidator远程检查数据库中的电子邮件可用性

时间:2016-03-23 12:49:11

标签: java twitter-bootstrap jsp servlets bootstrapvalidator

我正在尝试使用jsp和servlet中的bootstrapValidator检查数据库中是否有可用的电子邮件。但问题是它始终显示错误消息,即使电子邮件在数据库中不可用。

我的bootstrap form代码,

<div id="registration-form" class="modal fade" role="dialog">
<div class="modal-dialog registration-modal">
    <div class="modal-content">
        <form action="registerdone.jsp" method="post" class="form-horizontal" role="form"
              id="registrationFormValidation">
            <div class="modal-header">
                <a class="close cross" data-dismiss="modal">x</a>
                <h3>Register</h3>
            </div>
            <div class="modal-body">

                <div class="form-group">
                    <label class="col-md-3 control-label">Full Name</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control" id="name" name="name" placeholder="Your Name">
                    </div>
                </div>
                <!-- email -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Email</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control" id="email" name="email" placeholder="Your Email">
                    </div>
                </div>
                <!-- password -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Password</label>
                    <div class="col-md-8">
                        <input type="password" class="form-control" id="password" name="password"
                               placeholder="Enter Password">
                    </div>
                </div>
                <!--confirm password-->
                <div class="form-group">
                    <label class="col-md-3 control-label">Confirm Password</label>
                    <div class="col-md-8">
                        <input type="password" class="form-control" id="confirmpassword" name="confirmpassword"
                               placeholder="Confirm Password">
                    </div>
                </div>
                <!-- mobile -->
                <div class="form-group">
                    <label class="col-md-3 control-label">Mobile</label>
                    <div class="col-md-8">
                        <input type="text" class="form-control" id="mobile" name="mobile"
                               placeholder="Enter Mobile">
                    </div>
                </div>
            </div>
            <div class="modal-footer modal-footer-hidden-border">
                <a href="#" class="btn btn-link" data-toggle="modal" data-target="#login" data-dismiss="modal">Already
                    Registered ? Login</a>
                <button type="submit" value="submit" class="btn btn-success">Register
                </button>
            </div>
        </form>
    </div>
</div>
</div>

我的bootstrapValidator代码,

<script type="text/javascript">

 $(document).ready(function () {
    console.log("hiiiiiiii register validations");
    var validator = $("#registrationFormValidation").bootstrapValidator({
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: 'The full name is required'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The full name must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z\s]+$/,
                        message: 'The full name can only consist of alphabetical and spaces'
                    }
                }
            },
            email: {
                message: "Email is required",
                validators: {
                    notEmpty: {
                        message: "Please provide an email address"
                    },
                    stringLength: {
                        min: 6,
                        max: 35,
                        message: "Email must be between 6 and 35 characters long"
                    },
                    emailAddress: {
                        message: "Email address must be valid"
                    },
                    regexp: {
                        regexp: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
                        message: 'Not a valid email address'
                    },
                    remote: {
                        type: "POST",
                        url: 'RegistrationEmailCheck',
                        delay: 1000,
                        message: 'Email is already available "PLEASE LOGIN"'
                    }
                }
            }, //.email
            password: {
                validators: {
                    notEmpty: {
                        message: "Password is required"
                    },
                    stringLength: {
                        min: 8,
                        message: "Password must be 8 characters long"
                    },
                    different: {
                        field: "email",
                        message: "Email and Password must be different"
                    }
                }
            },
            confirmpassword: {
                validators: {
                    notEmpty: {
                        message: "Confirm Password field is required"
                    },
                    identical: {
                        field: "password",
                        message: "Confirm Password and Password must match"
                    }
                }
            },
            mobile: {
                validators: {
                    notEmpty: {
                        message: "Mobile Number is required"
                    },
                    numeric: {
                        message: "Not a valid phone number"
                    },
                    stringLength: {
                        min: 10,
                        max: 10,
                        message: "Only 10 digits are allowed"
                    },
                    regexp: {
                        regexp: /^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$/,
                        message: 'Enter a valid number'
                    }
                }
            }
        }//.fields
    });
});
</script>

我的servlet代码,

 import dbconnector.DBInfo;
 import org.json.simple.JSONObject;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;


 @WebServlet(name = "RegistrationEmailCheck")
  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(true);
           json.put("valid", true);
            System.out.println("true");
        } else {
            out.print(false);
           json.put("valid", false);
            System.out.println("false");
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
      out.print(json);
        out.close();
    }
out.print(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

使用ajax直接验证您的电子邮件,而不是使用remote方法的bootstrap验证器。

$('#testSubmit').click(function() {

    $.ajax({
        type : 'post',
        url : 'RegistrationEmailCheck',
        data: { email: 'sample@test.com'},
        success : function(data) {
            alert(data);
            console.log(data);
        }
    });
});

在HTML中,您也可以在同一元素中使用onblur函数。但它非常沮丧。所以我添加了按钮。

<button id="testSubmit" />

答案 1 :(得分:0)

我已经弄清楚我的代码有什么问题

问题是它没有从servlet返回有效的json数据。

将我的servlet更改为以下代码

     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();
    }



}


}

现在问题在于返回有效的json数据。