JQuery validation - work required but don’t accept

im upload image and try to check it before using jquery. Here is my code:

<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script> <script type='text/javascript'> $(document).ready(function(){ $("#form").validate({ errorLabelContainer: "#message_box", wrapper: "li", rules: { image: {required: true, accept: "jpg|jpeg|png|gif"} }, messages: { image: {required: 'Required!', accept: 'Not an image!'} } }) }); </script> 

It definitely works - if I do not insert an image, I get an error message. But to agree does not work (everything that I insert in the aisles), and I cannot understand why. Any ideas?:)

+4
source share
3 answers

You need two things.

(1) use valid syntax to use the accept method, because it requires that you use mimetypes to provide comma-separated lists of types.

 $(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 will need to enable additional-methods.js because accept methods are not included in the main validation plugin. So add the following to your <head> after enabling validate plugin

 <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> 

Here's a link to jsfiddle . Note that it includes debug: true to prevent the form from being published in the script.

+18
source

1) As already mentioned, you need to include the additional-methods.js file.

2) For file extensions, use the extension rule . The accept rule is for mime types.

 $(document).ready(function(){ $("#form").validate({ errorLabelContainer: "#message_box", wrapper: "li", rules: { image: { required: true, extension: "jpg|jpeg|png|gif" } }, ... }) }); 
+4
source

The documentation states that the accept rule only accepts mime types as an argument.

If you want to accept all images, use image/* .

If you want to accept only certain types of images, you can specify several types of mime, separating them with a comma, for example. image/pjpeg,image/jpeg,image/png,image/gif .

+2
source

All Articles