0

bootstrapValidatorjspとサーブレットを使用して、データベースで電子メールが利用可能かどうかを確認しようとしています。しかし、問題は、電子メールがデータベースで利用できない場合でも、常にエラー メッセージが表示されることです。

私の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 {
}
}

どんな助けでも大歓迎です。

4

2 に答える 2

1

ブートストラップ バリデーターのメソッドを使用する代わりに、remoteajax を使用してメールを直接検証します。

$('#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" />
于 2016-03-24T13:20:16.870 に答える
0

私は自分のコードの何が問題だったのかを理解しました

問題は、サーブレットから有効な 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();
    }



}


}

そして今、問題は有効なjsonデータの返還にありました。

于 2016-03-29T01:43:53.953 に答える