Search code examples
jqueryjquery-validatehttp-status-code-400

jQuery Validate required not working, I want know why


I am trying to validate my form submission using jqueryvalidation.js . the Browser response like that

     HTTP Status 400 -

     type Status report

     message

     description The request sent by the client was syntactically incorrect.

I use this code but validation not working

   <script type="text/javascript" src = "${contextPath }/static/js/jquery-migrate-1.4.1.min.js"></script>
        <script type="text/javascript">
                window.jQuery || document.write("<script src='${contextPath}/static/ace_admin1.3.1/assets/js/jquery.min.js'>"+"<"+"/script>");
        </script>

        <script type="text/javascript" src = "${contextPath }/static/js/jquery.validate.min.js"></script>
            <script type="text/javascript">
            $.validator.setDefaults({
                submitHandler: function() {
                  alert("sumbit");
                }
            });
                $(function() {
                    $("#subjectForm").validate({
                            rules: {
                                name : {
                                    required: true,
                                    },
                                testCount : {
                                    required: true,
                                    },
                                totalTime : {
                                    required: true,
                                    },
                                totalScore : {
                                    required: true,
                                    }
                                },
                            });
                        });
            </script>
    </head>
    <body>

                                <form id="subjectForm " action="${contextPath }/teacher/testAddSubject" method="POST">
                                <p>add</p>
                                <p><label>subjectName</label><input name = "name" type ="text"/></p>
                                <p><label>Description</label><input name = "description" type ="text"/></p>
                                <p><label>number</label><input name = "testCount" type ="text"/></p>
                                <p><label>time</label><input name = "totalTime" type ="text"/></p>
                                <p><label>grade</label><input name = "totalScore" type ="text" /></p>
                                <button type="submit">submit</button>
                                </form>

    </body>
    </html>

I don't know where is error, please help me.it about jQuery version and jQuery Validate version?


Solution

  • Remove the extra space at the end of...

    id="subjectForm "
    

    It's breaking your JavaScript. With broken JavaScript, jQuery Validate will not initialize.

    Working DEMO: jsfiddle.net/1crgtu7y/

    (You'd also need to include jQuery Migrate AFTER jQuery)


    As far as your status code 400:

    The request sent by the client was syntactically incorrect.

    This has absolutely nothing to do with the jQuery Validate plugin.

    This is something else entirely:

    400 Bad Request: The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

    So I suggest that you inspect the rendered DOM in your browser and make sure every server request (URL) is formed properly. Also, look at your server-side script at ${contextPath}/teacher/testAddSubject and make sure it's not broken.