2017-07-19 74 views
0

当我上传图像然后验证工作正常但当我如何使用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(); 
     } 
    }); 
} 
+0

尝试在更新页面中使用$(“[name = fileData]”)。(“remove”,“required”)删除所需的规则 –

回答

0

你的情况,你可以使用自己的自定义函数来验证该文件需要在第一次上传和更新时间

$(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 
      } 
     } 
    }); 
}); 
+0

我只是说更新图像假设我想先更新图像我必须从数据库获取数据并在编辑表单上显示现在让我们假设我没有更改任何图像,我是绝对的t点击更新按钮,然后错误即将到来字段是必需的如何管理它 –

+0

@ShahzadDitro在你的情况下,你应该写自己的自定义函数来做那个检查文件是否有价值,我更新我answr请检查 –