使用jQuery验证插件文件上传验证(File upload validation with jQu

2019-07-20 18:22发布

我有一个奇怪的疑难问题有一个文件上传和其他一些textarea的。每一个领域一个形式是required.so基本上当时很少字段为空,验证工作正常,但,当且仅当文件上传留空,表单被提交。

这里是我的代码

 <li >
  <p>
    <label for="rad">radio label:
    </label><br>

   <input type="radio" name="rad" value="yes" style="width:20px">&nbsp; Yes&nbsp;&nbsp;</input>
   <input type="radio" name="rad" value="no" style="width:20px">&nbsp; No</input><br/>
   <label for="cv" class="error" style="display:none">This field is required</label>
   </p>
    <p>
    <input type="file" name="fupl" style="display:none;margin-top:10px" id="fup"/>
    <label for="fupl" class="error" style="display:none;color:red">This field is required</label>
    </p>
    </li>
    <br>
    <li>
  <label>checkbox label
  </label><br><br>
 <input type="checkbox" name="cb" value="tick" style="width:20px">&nbsp;&nbsp;   <small>checkbox field<small></input><br>                                          <label for="fee" class="error" style="display:none">This field is required</label>
    </li>
  <br><li>
 <input type="submit" class="button" value="SUBMIT" style="float:right"/>
 </li>
 <script>
 $(document).ready(function()
 {
 $("input[type='radio']").change(function(){
if($(this).val()=="yes")
 {
 $("#fup").show();
 }
else
{
  $("#fup").hide();
 }
});
});

这我的jQuery

  $('#form').validate({
rules: {    
fupl: {
    required: true,
    accept:'docx|doc'
    },    

Answer 1:

您的代码似乎工作得很好。 隐藏字段由验证插件忽略。 然而,当你通过单选按钮显示文件上传字段,它会验证并显示错误消息。 请参阅: http://jsfiddle.net/y5ghU/


您的代码:

<input type="file" name="fupl" style="display:none;margin-top:10px" id="fup"/>

文件上传字段设置为display:none; 和插件将忽略默认情况下,所有的隐藏的字段。

使用ignore: []选项来禁用此功能。

$(document).ready(function () {

    $('#form').validate({
        ignore: [],
        rules: {
            fupl: {
                required: true,
                accept: 'docx|doc'
            }
        }
    });

});

DEMO: http://jsfiddle.net/ptV35/


编辑 :不能确定哪种方式OP想走,但下面的演示隐藏在文件上传字段重新隐藏任何未决的错误消息。

$("#fup").next('label.error').hide();

http://jsfiddle.net/y5ghU/4/



文章来源: File upload validation with jQuery Validate plugin