3

If the file size is more than 2MB or less than 2mb it always display File size more than 2mb

<script>
$.validator.addMethod('filesize', function(value, element, param) {

    var param = 2000;
    // element = element to validate (<input>)
    // value = value of the element (file name)

    return this.optional(element) || (element.files[0].size <= param)    
});

$("#dealDetails").validate({

    rules   : {
    "dealcatg"  : {
        required: true
    },

    "deal_image":{
        required : true,
        accept   : "image/*",
        filesize : true,
    }
});
</script>

Its always return false.

2
  • 1
    you return value in or condition, can you return value in separate and then check? return this.optional(element); console.log(element.files[0].size) Commented Sep 18, 2014 at 6:43
  • Make that console log on element.files[0].size as Prashant sugested and come back with the news please Commented Sep 16, 2015 at 13:00

1 Answer 1

1

You can add a jquery validation method like shown below

jQuery.validator.addMethod("checksize", function (val, element) {

      var size = element.files[0].size;
        console.log(size);

       if (size > 2*1048576)// checks the file more than 1 MB
       {
           console.log("returning false");
            return false;
       } else {
           console.log("returning true");
           return true;
       }

  }, "File type error");

and use it like below

 $(document).ready(function(){
 $('#form').validate({
  rules: {
       image: {
          required: true,
          extension:'jpe?g,png',
          checksize:true,
          }
         }
      });
   });

Try this..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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