0

Hi guys i have registration form designed in bootstrap where i have validate all the form fileds using remote validator and i have registerdao.java method which has checkusername boolean method which checks all the rows and if username exists it returns username string otherwise false and i have checkusernameexists method in registartiion servlet which checks username when you enter username fields.I don't understand why my code is not showing anything.Below is the code is not checking username against database and notifying.How to write javascript so that it checks the username against data or give me some ideas how to make this code work? any help would be greatly appreaciated?thanks

ReegisterDao.java public static boolean checkUserName(String userName) throws SQLException{ String checkUserNameQuery = "select * from EmployeeDetails where USER_NAME='"+userName+"'";

  try{


 // preparing some objects for connection
currentCon = ConnectionManager.getConnection();
Statement stmt = currentCon.createStatement();
rs  = stmt.executeQuery(checkUserNameQuery);
boolean more = rs.next();

    // if user does not exist set the isValid variable to false
    return more;
     }



catch (InstantiationException | IllegalAccessException e){}

return false; }

RegistrationServlet.java

public int checkUserNameExists(RegistrationBean user) throws SQLException{

        int a=RegistrationDao.Register(user);


    return a;

}

this is part of remote validator which validates username

   username: {
                    validators: {
                        notEmpty: {
                            message: 'The username is required'
                        },
                        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:{
                            message:'This Username is already taken',
                            url:'https://localhost:8080/RegistrationServlet.java',
                            data:'username'
                        },
                        type:'POST'


                }   

                },

and this is part of bootstrap username fields

<div class="form-group">
    <label class="col-xs-2 control-label">Username</label>

    <div class="col-xs-4">
        <input type="text" class="form-control" name="username" placeholder="Username" />
    </div></div>
Dipen
  • 1
  • 5

1 Answers1

0

the return type Must be in valid json format . And I think whatever u r returning from servlet to that validation code is not in correct json form. use inspect element and in network tab see whats it is returning.

First you must change ur remote to like this

             remote: {
                        type: "POST",
                        url: 'RegistrationEmailCheck',//servlet
                        delay: 1000,

                        message: 'Your Message'
                    }

and from servlet return data like this

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



}


}

This code is for email checking

thedudecodes
  • 1,479
  • 1
  • 16
  • 37