Jquery文件上载大小检查

时间:2015-11-06 06:59:36

标签: jquery

licence: {
                required:true,
                accept: "application/pdf",
                size: 1MB,
            }

我想验证名为license的文件上传输入标签。最大大小应为1 MB,大于1MB时应显示错误。如何编写?我已经有它的大小:1MB。这是对的吗?

2 个答案:

答案 0 :(得分:0)

在哪里写这段代码?

这个变量fsize是什么?

$( “#vendor_registerationfrm”)。验证({

<script async defer
  src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places&key=THISISMYAPIKEYBLABLABLA&callback=initMap">
</script>

许可证:{                 要求:真实,                 接受:“application / pdf”,                 大小:1MB,             } }

我的代码看起来像这样..不需要写大小属性? ?

答案 1 :(得分:0)

 var baseurl = $("meta[name=baseurl]").attr("content");

 $(window).load(function() {
     // Animate loader off screen
     $(".page-loader").fadeOut("slow");
 });

 $.validator.addMethod(
     "formatDate",
     function(value, element) {
         //console.log(value);        
         // put your own logic here, this is just a (crappy) example
         return value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
     },

     "Please enter a date in the format dd/mm/yyyy."
 );

 $('.number').inputmask("99999", {
     placeholder: "",
     clearMaskOnLostFocus: true
 });

 $('.dateinpt').inputmask("dd/mm/yyyy", {
     placeholder: "DD/MM/YYYY",
     clearMaskOnLostFocus: false
 });

 $('.decimal').inputmask("decimal", {
     allowMinus: false
 });

 $(".phone").inputmask("999999999999", {
     allowMinus: false
 });

 $('document').ready(function() {
     $("#vendor_registerationfrm").validate({
         rules: {
             mobile:
             {
                 //validphone: true,
                 minlength: 5,
                 maxlength: 15,
                 number: true,
                 required: true,
                 remote: {
                     url: baseurl + "crm/mobile_check_ajax",
                     type: "post",
                     data: {
                         mobile: function() {
                             return $("#mobile").val();
                         }
                     }
                 }
             },

             name: {
                 required: true,
                 minlength: 3,
             },

             email: {
                 required: true,
                 email: true,
                 remote: {
                     url: baseurl + "crm/email_check_ajax",
                     type: "post",
                     data: {
                         email: function() {
                             return $("#email").val();
                         }
                     }
                 }
             },

             licence: {
                 required: true,
                 accept: "application/pdf",
                 size: 1 KB,
             },


             expiry: {
                 formatDate: true,
             },

             vend_logo: {
                 accept: "image/jpeg, image/pjpeg, image/png",
             },

             banner: {
                 accept: "image/jpeg, image/pjpeg, image/png"
             },

         },

         messages: {
             mobile: {
                 remote: 'Mobile already used!'
             },

             email: {
                 remote: 'Email already used!'
             },

             licence: {
                 accept: '&nbsp;Invalid file type'
             },

             vend_logo: {
                 accept: '&nbsp;Invalid image'
             },

             banner: {
                 accept: '&nbsp;Invalid image'
             },
         }
     });

     $("#vendor_editfrm").validate({
         rules: {
             mobile:
             {
                 //validphone: true,
                 minlength: 5,
                 maxlength: 15,
                 number: true,
                 required: true,
                 remote: {
                     url: baseurl + "crm/mobile_check_edit_ajax",
                     type: "post",
                     data: {
                         mobile: function() {
                             return $("#mobile").val();
                         },

                         ajaxid: function() {
                             return $("#ajaxid").val();
                         }
                     }
                 }
             },

             name: {
                 required: true,
                 minlength: 3,
             },

             email: {
                 required: true,
                 email: true,
                 remote: {
                     url: baseurl + "crm/email_check_edit_ajax",
                     type: "post",
                     data: {
                         email: function() {
                             return $("#email").val();
                         },

                         ajaxid: function() {
                             return $("#ajaxid").val();
                         }
                     }
                 }
             },

             licence: {
                 accept: "application/pdf"
             },

             expiry: {
                 formatDate: true,
             },

             vend_logo: {
                 accept: "image/jpeg, image/pjpeg, image/png",
             },

             banner: {
                 accept: "image/jpeg, image/pjpeg, image/png"
             },
         },

         messages: {
             mobile: {
                 remote: 'Mobile already used!'
             },

             email: {
                 remote: 'Email already used!'
             },

             licence: {
                 accept: '&nbsp;Invalid file type'
             },


             vend_logo: {
                 accept: '&nbsp;Invalid image'
             },

             banner: {
                 accept: '&nbsp;Invalid image'
             },
         }
     });

     jQuery.validator.addMethod(
         'validphone',
         function(value) {
             return /[0-9]{10}/.test(value);
         },
         'Please enter valid mobile number.'
     );
 });

 $("#vendor_editfrm_vendor").validate({
     rules: {
         mobile:
         {
             //validphone: true,
             minlength: 5,
             maxlength: 15,
             number: true,
             required: true,
             remote: {
                 url: baseurl + "vendor/mobile_check_edit_ajax",
                 type: "post",
                 data: {
                     mobile: function() {
                         return $("#mobile").val();
                     },

                     ajaxid: function() {
                         return $("#ajaxid").val();
                     }
                 }
             }
         },

         name: {
             required: true,
             minlength: 3,
         },

         // licence: {
         //     accept: "application/pdf"
         // },

         vend_logo: {
             accept: "image/jpeg, image/pjpeg, image/png",
         },

         banner: {
             accept: "image/jpeg, image/pjpeg, image/png"
         },

     },

     messages: {
         mobile: {
             remote: 'Mobile already used!'
         },

         email: {
             remote: 'Email already used!'
         },

         licence: {
             accept: '&nbsp;Invalid file type'
         },

         vend_logo: {
             accept: '&nbsp;Invalid image'
         },

         banner: {
             accept: '&nbsp;Invalid image'
         },
     }
 });

 $(function() {
     $('#ms').change(function() {
         //console.log($(this).val());
     }).multipleSelect({
         width: '100%'
     });

     $('#ms2').change(function() {
         //console.log($(this).val());
     }).multipleSelect({
         width: '100%'
     });

     $("#vend_logo").change(function() {
         // $("#dvPreview").html("");
         var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;

         if (regex.test($(this).val().toLowerCase())) {
             // if ($.browser.msie && parseFloat(jQuery.browser.version) <= 9.0) {
             //     $("#dvPreview").show();
             //     $("#dvPreview")[0].filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = $(this).val();
             // }
             // else {
             if (typeof(FileReader) != "undefined") {
                 //  $("#dvPreview").show();
                 // $("#dvPreview").append("<img width='337' />");
                 var reader = new FileReader();
                 reader.onload = function(e) {
                     $("#dvPreview img").attr("src", e.target.result);
                 }

                 reader.readAsDataURL($(this)[0].files[0]);
             } else {
                 alert("This browser does not support FileReader.");
             }
             // }
         } else {
             alert("Please upload a valid image file.");
         }
     });

     $("#banner_img").change(function() {
         var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
         if (regex.test($(this).val().toLowerCase())) {
             if (typeof(FileReader) != "undefined") {
                 var reader = new FileReader();
                 reader.onload = function(e) {
                     $("#bannerPreview img").attr("src", e.target.result);
                 }
                 reader.readAsDataURL($(this)[0].files[0]);
             } else {
                 alert("This browser does not support FileReader.");
             }
         } else {
             alert("Please upload a valid image file.");
         }
     });

     $("#promotions1_inp").change(function() {
         var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
         if (regex.test($(this).val().toLowerCase())) {
             if (typeof(FileReader) != "undefined") {
                 var reader = new FileReader();
                 reader.onload = function(e) {
                     $("#promotions1Preview img").attr("src", e.target.result);
                 }
                 reader.readAsDataURL($(this)[0].files[0]);
             } else {
                 alert("This browser does not support FileReader.");
             }
         } else {
             alert("Please upload a valid image file.");
         }
     });

     $("#promotions2_inp").change(function() {
         var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
         if (regex.test($(this).val().toLowerCase())) {
             if (typeof(FileReader) != "undefined") {
                 var reader = new FileReader();
                 reader.onload = function(e) {
                     $("#promotions2Preview img").attr("src", e.target.result);
                 }
                 reader.readAsDataURL($(this)[0].files[0]);
             } else {
                 alert("This browser does not support FileReader.");
             }
         } else {
             alert("Please upload a valid image file.");
         }
     });
 });