Suc*_*Man 2 javascript jquery jquery-validate
我的 jquery 验证是这样的:
store: {
rules: {
banner: {
required: true
}
},
messages: {
banner: 'The banner must be filled',
},
errorElement: 'span',
errorPlacement: errorPlacement,
highlight: highlight,
unhighlight: unhighlight
},
Run Code Online (Sandbox Code Playgroud)
如果用户不上传尺寸为 1170 x 300 像素的横幅,则会出现如下消息:
请上传一张 1170 x 300 像素尺寸的图片
我该怎么做?
您需要创建自定义验证方法
$.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');
Run Code Online (Sandbox Code Playgroud)
并且您需要像这样在图像更改时设置参数imageWidth和imageHeight
// files 是您输入的 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);
}
});
Run Code Online (Sandbox Code Playgroud)
并像这样打电话
rules: {
banner: {
dimention:[1170,300]
}
}
Run Code Online (Sandbox Code Playgroud)
工作小提琴链接小提琴链接
| 归档时间: |
|
| 查看次数: |
1708 次 |
| 最近记录: |