1

I've written a upload form and I want users to be able to upload only images. I've decided to use jQuery validator in order to make sure that users cannot send an empty form an can only upload images. So far, users indeed can't send an empty form, but they can upload and file they would like to , even if it's not an image. Here's the code of jsFunc.js which aim to handle this problem:

$(document).ready(function () {
$('#upload-image').validate({
    errorElement : "span",
    errorPlacement: function(error, element) {
        error.appendTo(".upload-error");
    },
    rules: {
        pic: {
            required: true,
            accept: "image/*"
        }
    }
});
});

Here are the files included in the page that included the upload form :

<script src="bootstrap/js/jquery.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
<script type="text/javascript" src="sys/jsFunc.js"></script>
<script type="text/javascript" src="sys/additional-methods.js"></script>
<script type="text/javascript" src="sys/jquery.validate.js"></script>

What is wrong with the code above? I'd be grateful if anyone could point me to the problem.

3
  • seems fine to me jsfiddle.net/arunpjohny/9kYYH/1 Commented May 9, 2013 at 6:48
  • @ArunPJohny , thanks , I've also checked it using jsfiddle and that's why it seems strange. I've not edited the post and included the files i'm importing, maybe something it wrong / missing ? Commented May 9, 2013 at 6:53
  • The order of imports is wrong jquery.validate.js first then additional-methods.js Commented May 9, 2013 at 6:57

3 Answers 3

1

The order of script file imports is wrong, jquery.validate.js first then additional-methods.js

<script type="text/javascript" src="sys/jquery.validate.js"></script>
<script type="text/javascript" src="sys/additional-methods.js"></script>
Sign up to request clarification or add additional context in comments.

2 Comments

First of all , thanks for the answer ! I'd like to know please why there is any importance for the order of file importing ?
because the additional-methods files uses features included in the validate.js file. so when the additional-methods file is executed it expects the validate.js file to be already executed
1

If you're trying to validate by file extension or to accept only images with certain extension you can use like this

rules: {
   field: {
     required: true,
     extension: "jpeg|png"
   }
}

See: http://docs.jquery.com/Plugins/Validation/CustomMethods/extension#extension

2 Comments

Could you please share jsfiddle for it?
Please how would you collect the values of the file field and the text field into another page after validating them
0
rules: {
   field: {
     required: true,
     accept: "jpeg|png"
   }
}

Replace extension with accept, it 'll work

Comments

Your Answer

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