Here is html code for the form:
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.
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.
Is the script in correct order ? jquery followed by validate followed by unobstrusive.
Is the $.validator function defined ? Check quickly in the console of the browser
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.
Hope this helps.
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.
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;
});
This error happened because I had another html element with same id. After removing that id code worked fine.
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.