我的jquery验证如下:
store: {
rules: {
banner: {
required: true
}
},
messages: {
banner: 'The banner must be filled',
},
errorElement: 'span',
errorPlacement: errorPlacement,
highlight: highlight,
unhighlight: unhighlight
},如果用户没有上传尺寸为1170x300像素的横幅,则存在如下消息:
请上传一张尺寸为1170 x 300像素的图片。
我该怎么做呢?
发布于 2017-11-29 07:53:13
您需要创建自定义验证方法。
$.validator.addMethod('dimention', function(value, element, param) {
if(element.files.length == 0){
return true; <--- check here if file not added than return true for not check file dimention
}
var width = $(element).data('imageWidth');
var height = $(element).data('imageHeight');
if(width == param[0] && height == param[1]){
return true;
}else{
return false;
}
},'Please upload an image with 1170 x 300 pixels dimension');您需要在图像更改时设置参数imageWidth和imageHeight,如下所示
//文件是输入的id
<input type="file" id="files" name="name" />
$('#files').change(function() {
$('#files').removeData('imageWidth');
$('#files').removeData('imageHeight');
var file = this.files[0];
var tmpImg = new Image();
tmpImg.src=window.URL.createObjectURL( file );
tmpImg.onload = function() {
width = tmpImg.naturalWidth,
height = tmpImg.naturalHeight;
$('#files').data('imageWidth', width);
$('#files').data('imageHeight', height);
}
});然后像这样打电话
rules: {
banner: {
dimention:[1170,300]
}
}工作小提琴链接琴杆
https://stackoverflow.com/questions/47547534
复制相似问题