Search code examples
javatwitter-bootstrapjspservletsbootstrapvalidator

Remote checking of email availability in database using bootstrapValidator in Jsp Servlet


I am trying to check if a email is available in database using bootstrapValidator in jsp and servlet. But the problem is that it is always showing a error message even email is not available in database.

My bootstrap form code,

<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>

My bootstrapValidator code,

<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>

my servlet code,

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

Any help is appreciated.


Solution

  • I have figured out what was the problem with my code

    the problem was that it was not returning the valid json data from servlet.

    changing My servlet to the below code

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

    And now its working the problem was with the return of the valid json data.