jQuery validation - required working but accept not

后端 未结 3 1428
伪装坚强ぢ
伪装坚强ぢ 2021-01-05 17:33

im uploading an image and trying to validate it before with jquery. Here is my code:



        
3条回答
  •  渐次进展
    2021-01-05 18:05

    You need two things.

    (1) use valid syntax for using accept method because it requires you to use to provide comma-separated list of mimetypes.

    $(document).ready(function(){
        $("#form").validate({
            errorLabelContainer: "#message_box", wrapper: "li",
    
            rules: {
                image: {required: true, accept: "image/jpg,image/jpeg,image/png,image/gif"}
            },
            messages: {
                image: {required: 'Required!', accept: 'Not an image!'}
            }
        })
    });  
    

    (2) You'll have to include the additional-methods.js because the accept methods is not included in the core validate plugin. So add following to your after you include validate plugin

    
    

    Here's the link to jsfiddle. Note that it includes debug: true to prevent posting of the form in fiddle.

提交回复
热议问题