jQuery validation plugin error: TypeError: validator is undefined

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

Here is html code for the form:

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

    Because this is the first topic that Google shows for "validator is undefined" then I will describe my solved problem, maybe someone will find it helpfull.

    I was using 'maxlength' attribute (nothing fancy) and I got this error. The reason was nested forms. One of the textarea was inside both forms. Can happen when you have modals (dialogs) with forms in main view.

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

    For anyone still looking for an answer and the answers above dont work..

    I spent the last 3 hours trying all the solution and none of them worked.

    I finally realized a very stupid mistake , I had initialized Jquery twice in my application

    once in the head where i had included 3 scripts

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
        <script src="~/Scripts/jquery.validate.min.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
    

    and once again at the end of the page where i only had the line.

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    

    This was resetting the validator function of $ and causing everything to break.

    Some things to check for cases where validations are not firing.

    1. Is the script in correct order ? jquery followed by validate followed by unobstrusive.

    2. Is the $.validator function defined ? Check quickly in the console of the browser

    3. Is the form to be validated being created dynamically ? If so , you may need to re initialize the validator plugin on the form , the best way i have found of doing this are mentioned below , choose the one which fits well for you.

      $.validator.unobstrusive.parse('#myForm')
      

    or

     $('#myForm').validate() // this just makes the validator plugin consider the form for unobstrusive validation
    

    or

    $('#myForm').valid() // this will trigger unobstrusive validation on the form.
    
    1. If you have complicated ajax loads for many forms , make sure that the scripts are not included again and again in each ajax page , also make sure that elements on the main page still have unique names.

    Hope this helps.

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

    In my case the problem came that had two instanced versions of Jquery UI i JQuery. This made that Jquery normal code works fine but delete the intance of $.validator.

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

    Same happened here. For me it was a typo in my code:

    $(document).ready(function(){
        //START of validate
    $('#reply').validate({
        rules:{
            message:{
                required: true,
                },  
            },
        submitHandler: function(form) {
            var data = $("#reply").serialize();
            $.ajax({
                type:"POST",
                url:"ajax/scripts/msg_crt.php",
                data:data,
                success:function(data){
                    alert("Loaded");
                    }
                });
            }
        });
        //END of validate
      });
    
    $(document).on('click','#send', function(){
        if($('#replay').valid()){// <--- Here is my selector typo
            $("#replay").submit(); // <--- Here is my selector typo
            }
            return false;
        });
    
    0 讨论(0)
  • 2020-12-09 03:30

    This error happened because I had another html element with same id. After removing that id code worked fine.

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

    In my case I was calling $.validator before $(document).ready. Once I moved it to a function being called from ready, it worked. It will work after or during ready but not before.

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