How Define jQuery Validate setDefaults

I have a problem with my code. I use jquery validate to validate my form. My question is how to run the setDefaults method? Because when looking at it in the debugger, I get an error like this: TypeError: jQuery.validator is undefined

jQuery.validator.setDefaults({
            success : "valid"
        });
        $("#myForm").validate({
            rules : {
                "file1" : {
                    required : true,
                    extension : "slx|mdl",
                    accept : false
                },
                "file2" : {
                    extension : "m",
                    accept : false
                },
                "file3" : {
                    extension : "xlsx|xls",
                    accept : false
                },
                "file4" : {
                    extension : "pdf",
                    accept : false
                }
              }
        });

2 Answers

setDefaults need a variable which must be used to validate in your case validator

just assign validator var to your validate method like this

var validator = $("#myForm").validate({
    rules : {
        "file1" : {
            required : true,
            extension : "slx|mdl",
            accept : false
        },
        "file2" : {
            extension : "m",
            accept : false
        },
        "file3" : {
            extension : "xlsx|xls",
            accept : false
        },
        "file4" : {
            extension : "pdf",
            accept : false
        }
      }
});

should work fine

3

Check whether you added jquery validate lib correctly:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest