0

I an using jquery validator plugin to validate my registration form. However, when I tried to check if an username is in the database, it does not work. Can anyone help take a look at my codes and let me know what I did wrong? It always shows user already exist even when the username is not in database.

I have tried the methods from

jQuery Validate remote method usage to check if username already exists and JQuery Remote Validation not displaying Error

but it still doesn't work.

My registration jquery function:

   $().ready(function() {

        $.validator.addMethod('uniqueUsername', function(value) {
            $.ajax({
                url: 'http://localhost:8080/TBS-war/RegisterServlet.java',
                type: 'POST',
                async: false,
                contentType: 'application/json',
                dataType: 'json'

            });
        });

        //Validate signup form on keyup and submit
        $("#registrationForm").validate({
            // Set rules
            rules: {
                username: {
                    required: true,
                    uniqueUsername: true
                    /*remote: {
                        url: "http://localhost:8080/TBS-war/RegisterServlet.java",
                        type: "post",
                        contentType: "application/json",
                        data: {
                            'username': $('#username').val()
                        }
                    }*/
                },
                password: {
                    required: true
                },
                confirmPassword: {
                    required: true,
                    equalTo: "#password"
                },
                contact: {
                    required: true
                },
                email: {
                    required: true,
                    email: true
                },
                address: {
                    required: true
            }},

            // Set messages
            messages: {
                username: {
                    required: "* This field is required",
                    uniqueUsername: "* User already exist"
                    //remote: "* User already exist"
                },
                password: {
                    required: "* This field is required"
                },
                confirmPassword: {
                    required: "* This field is required",
                    equalTo: "* Please enter the same password as above"
                },
                contact: {
                    required: "* This field is required"
                },
                email: {
                    required: "* This field is required",
                    email: "* Please enter a valid email"
                },
                address: {
                    required: "* This field is required"
            }},

My servlet file:

@EJB
private TBSManagerBeanRemote tBSManagerBean;


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/json");

        PrintWriter out = response.getWriter();
        JSONObject json = new JSONObject();
        String username = request.getParameter("username");

        if (tBSManagerBean.userExist(username)) {
            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");
        }
    }
Community
  • 1
  • 1
Shaoz
  • 3
  • 4
  • When using the `.addMethod()` method, you must `return` a `true` or `false` depending on whether you want to pass or fail validation. Your custom method returns nothing. See examples: https://jqueryvalidation.org/jQuery.validator.addMethod/ – Sparky Mar 24 '17 at 21:26
  • However, the most proper way to do this is through the `remote` method. And you do not need the `data` option because the value of the field is already sent. See examples: https://jqueryvalidation.org/remote-method/ – Sparky Mar 24 '17 at 21:28
  • I tried the remote method but when using the remote method, the validation does not work – Shaoz Mar 24 '17 at 21:40
  • And may I know how can I compare the values returned by the servlet in the jquery? I tried to find it online but don't really understand how it was done – Shaoz Mar 24 '17 at 21:45

0 Answers0