Javascript 如何使用jquery validate添加以像素为单位的验证维度?
我的jquery验证如下:Javascript 如何使用jquery validate添加以像素为单位的验证维度?,javascript,jquery,jquery-validate,Javascript,Jquery,Jquery Validate,我的jquery验证如下: store: { rules: { banner: { required: true } }, messages: { banner: 'The banner must be filled', }, errorElement: 'span', errorPlacement: errorPlacement, highlight: highlig
store: {
rules: {
banner: {
required: true
}
},
messages: {
banner: 'The banner must be filled',
},
errorElement: 'span',
errorPlacement: errorPlacement,
highlight: highlight,
unhighlight: unhighlight
},
如果用户未上传尺寸为1170 x 300像素的横幅,则存在如下消息:
store: {
rules: {
banner: {
required: true
}
},
messages: {
banner: 'The banner must be filled',
},
errorElement: 'span',
errorPlacement: errorPlacement,
highlight: highlight,
unhighlight: unhighlight
},
请上传尺寸为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');
使用
jQuery.validator.addMethod
工作FIDLE链接,并执行像素检查。存在错误:Uncaught TypeError:无法读取未定义的属性“0”。检查元素时出现异常,请检查“dimention”方法
我已更改我的答案,您可以这样做,它完全符合您的要求。它起作用了。谢谢,我想问一下。如何使横幅不是必需的?我有更新我的答案删除需要和检查在尺寸方法,如果文件添加比检查尺寸
rules: {
banner: {
dimention:[1170,300]
}
}