jQuery validation plugin error: TypeError: validator is undefined

后端 未结 13 2311
无人及你
无人及你 2020-12-09 03:10

Here is html code for the form:

相关标签:
13条回答
  • 2020-12-09 03:12

    The same issue showed up for me. The reason for that error what that I was using the code:

    if ($("#invalidFormId").valid()) {
    ....
    }
    

    And the id of the form element was not valid

    0 讨论(0)
  • 2020-12-09 03:12

    The plugin comes in separate libraries, you need to include those in your html.

    <script src="../jquery-validation/dist/jquery-validate.min.js"></script>
    <script src="../jquery-validation/dist/additional-methods.min.js"></script>
    
    0 讨论(0)
  • 2020-12-09 03:13

    You need to include the validation script in the head of your document.

    Like this:

    <script src="/js/libs/jquery.validate.js" type="text/javascript"></script>
    

    Also, please check in the rendered HTML that your form ID is correct.

    I use this code for calling validate on my sites

            //Validate Form
            var ids = [];
            $("form").each(function () {
                ids.push(this.id);
            });
            var formId = "#" + ids[0]
            $(formId).validate();
    
    0 讨论(0)
  • 2020-12-09 03:14

    In my case the error was because the validator was being called too early. I moved the code to a seperate function and called it using delay method of underscore.js. Worked like a charm.

    _.delay(doValidations);
    
    function doValidations() {
    ...
    });
    
    0 讨论(0)
  • 2020-12-09 03:15

    I had @Scripts.Render("~/bundles/jquery") in the parent view (_Layout.cshtml) and had also defined some other jquery Script tags in another view, they clashed.

    0 讨论(0)
  • 2020-12-09 03:15

    This issue was occurring for me when calling validator.destroy() - the error was occurring within the staticRules method of the plugin. The cause turned out to be the associated form element (element.form) was detached from the DOM. I resolved it by checking the element was in the DOM before calling destroy, using the following method:

    document.contains(element)
    

    Hopefully this helps someone.

    0 讨论(0)
提交回复
热议问题