[英]How to validate Input type file when update image using Jquery?
當我上傳圖片時,驗證工作正常,但是當我
我要驗證的更新圖像輸入類型文件是否為空。 如何檢查
它?
其實我想使用更新過程中用於上傳圖片(創建圖片)的相同驗證功能嗎?
我該怎么辦?驗證功能就像:->
var teacherValidation = function() {
// for more info visit the official plugin documentation:
// http://docs.jquery.com/Plugins/Validation
var form1 = $('#form_teacher');
var error1 = $('.alert-danger', form1);
var success1 = $('.alert-success', form1);
form1.validate({
errorElement: 'span', //default input error message container
errorClass: 'help-block help-block-error', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "", // validate all fields including form hidden input
messages: {
select_multi: {
maxlength: jQuery.validator.format("Max {0} items allowed for selection"),
minlength: jQuery.validator.format("At least {0} items must be selected")
}
},
rules: {
fileData: {
required: true
}
},
submitHandler: function (form) {
success1.show();
error1.hide();
form.submit();
}
});
}
您可以使用自己的自定義功能來驗證文件在首次上傳和更新時是否需要
$(document).ready(function(){
jQuery.validator.addMethod("filetype", function(value, element) {
var types = ['jpeg', 'jpg', 'png'],
ext = value.replace(/.*[.]/, '').toLowerCase();
if (types.indexOf(ext) !== -1) {
return true;
}
return false;
},"Please select allowed file");
$('#frm').validate({
rules:
{
file:
{
filetype: true
}
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.