【问题标题】:How to make file upload 'Required' in Contact Form 7?如何在联系表 7 中使文件上传为“必需”?
【发布时间】:2018-07-02 02:06:32
【问题描述】:

我的 Wordpress 网站上有一个 Contact Form 7 表单,我已通过 * 指定文件上传是强制性的,但是用户可以在不上传文件的情况下提交表单。

相关的联系表格7表格如下:

<div class="filelink">[file* AddanImage filetypes:gif|png|jpg|jpeg]</div>

如何编写 Contact Form 7 代码或添加验证,以使用户在不上传图片的情况下无法提交表单?

非常感谢任何提示/帮助/指导。

【问题讨论】:

标签: wordpress contact-form-7


【解决方案1】:

您可以使用 JavaScript 检查文件的值是否存在。

const form = document.querySelector('form'),
      span = document.createElement("span"),
      text = document.createTextNode("Please upload a file: "),
      parent = document.getElementById("file").parentNode,
      file = document.getElementById('file');
      
form.addEventListener('submit', (e) => {
  if (file.value === '') {
    e.preventDefault();
    parent.insertBefore(span, file);
    span.appendChild(text);
  }
});
<form>
  <input type="file" name="file" id="file"><br>
  <input type="submit" value="Upload the file">
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多