如果文件大小超过2MB或小于2mb 它总是显示文件大小超过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>
它总是返回假。
答案 0 :(得分:1)
您可以添加如下所示的jquery验证方法
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");
并使用如下
$(document).ready(function(){
$('#form').validate({
rules: {
image: {
required: true,
extension:'jpe?g,png',
checksize:true,
}
}
});
});
试试这个..