Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate accept method - TypeError: Cannot read property 'call' of undefined

I want to check the upload file type using the validate plugin.

But I got the following error message:

Uncaught TypeError: Cannot read property 'call' of undefined.
Exception occurred when checking element file, check the 'accept' method.

Here is my form.

<form id="upload" enctype="multipart/form-data">
    <div class="control-group">
        <div class="controls">
            <input id="file" name="file" type="file">
        </div>
        <div class="form-group">
            <button class="btn btn-primary" onclick="Submit()" type="button">Submit</button>
        </div>
    </div>
</form>

And here is my JavaScript:

var Submit=function(){
    var validator = $('#upload').validate({
        rules:{
            file: {
                required: true,
                accept: "audio/*, video/*"
            }
        },
        message: {
            file:"Invalid file type"
        }
   });

    validator.form();
}
like image 801
user3927463 Avatar asked Nov 04 '15 20:11

user3927463


2 Answers

To use the "accept" method you'll need to include the jQuery Validate additional methods file. Simply include it after your validate file:

<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>

Then try initiating your validator like this instead:

$(document).ready(function(){
    $("#upload").validate({
        ...settings...
    });
});

And remove the onClick and type="button" attribute from your button.

Here are the docs:

http://jqueryvalidation.org/validate/

like image 87
Josh KG Avatar answered Sep 21 '22 18:09

Josh KG


I had same issue but I got a solution I have added the below code in script above jquery validation script

$.validator.addMethod('filesize', function (value, element, param) {
    return this.optional(element) || (element.files[0].size <= param)
}, 'File size must be less than {0}');

After that you have to add your own validation script

var Submit=function(){
    var validator = $('#upload').validate({
        rules:{
            file: {
                required: true,
                accept: "audio/*, video/*"
            }
        },
        message: {
            file:"Invalid file type"
        }
   });

    validator.form();
}
like image 1
Sumit Kumar Gupta Avatar answered Sep 19 '22 18:09

Sumit Kumar Gupta