formvalidation 仅在有文件上传时验证

formvalidation validate only if there is file uploaded

您好,我正在使用这个插件 formvalidation,我是新手,我有一个包含文件上传的表单,我想检查是否有正在上传的文件,如果有,我会验证这个接受(jpeg、png、gif)。如果有 none 我不会验证。

<form id="myform">
  <div class="form-group">
    <label for="fname">First Name</label>
    <input type="text" class="form-control" id="name" name="fname" placeholder="Name">
  </div>
  <div class="form-group">
    <label for="pic">Upload here</label>
    <input type="file" id="pic">

  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

提前致谢。

FormValidator 需要 data-fv-file-type 选项。

The allowed MIME type, separated by a comma. For example: Setting image/jpeg,image/png,application/pdf only allows to upload JPEG, PNG image and PDF document.

data-fv-file-type="image/jpeg,image/png,application/gif"一样使用它来在上传时验证文件扩展名。

此外,请记住通过设置 data-fv-file="true"

来启用验证器

例子

<input type="file" class="form-control" name="avatar"
            data-fv-file="true"
            data-fv-file-extension="jpeg,png"
            data-fv-file-type="image/jpeg,image/png"
            data-fv-file-maxsize="2097152"
            data-fv-file-message="The selected file is not valid" />