验证不适用于引导程序文件上载

时间:2017-07-02 12:41:34

标签: jquery html twitter-bootstrap validation bootstrap-file-upload

我使用的是bootstrap文件样式。我已经设置了Jquery验证,它正在运行,但我不知道我的代码中的问题。我正在上传图片jpg,png,jpeg,gif,但上传图片后验证仍然显示错误。

我的意思是我无法上传图片,因为我收到验证错误,但我使用的是正确的图片。我正在上传jpg图片但仍显示错误"File must be JPG, GIF or PNG"。你能帮帮我吗?



$(":file").filestyle({buttonBefore: true,buttonText: "Upload Image"});
	
	     function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah')
                        .attr('src', e.target.result)
                        .width(250)
                        .height(200);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }


	 $(function() {
  $("form[name='registration']").validate({

    // Specify the validation rules
    rules: {
      fileToUpload: {
        required: true,
        accept: "png|jpe?g|gif"
      }
    },
    // Specify the validation error messages
    messages: {
      fileToUpload: {
        required: "Please upload  image",
        accept: "File must be JPG, GIF or PNG"
      }
    },

    submitHandler: function(form) {
      form.submit();
    }
  });
});

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-filestyle/1.2.1/bootstrap-filestyle.min.js"></script>


<form name="registration" method="post" action="process.php">
<input type='file' onchange="readURL(this);" class="filestyle input-field" data-buttonBefore="true" data-buttonText="Upload Image" name="fileToUpload" id="fileToUpload" />

  <input type="submit" name="submit" value="submit">
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.16.0/additional-methods.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-filestyle/1.2.1/bootstrap-filestyle.min.js"></script>


<form name="registration" method="post" action="process.php">
<input type='file' onchange="readURL(this);" class="filestyle input-field" data-buttonBefore="true" data-buttonText="Upload Image" name="fileToUpload" id="fileToUpload" />

  <input type="submit" name="submit" value="submit">
</form>
<script>

$(":file").filestyle({buttonBefore: true,buttonText: "Upload Image"});
	
	     function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

                reader.onload = function (e) {
                    $('#blah')
                        .attr('src', e.target.result)
                        .width(250)
                        .height(200);
                };

                reader.readAsDataURL(input.files[0]);
            }
        }


	 $(function() {
	 //console.log(validate())
	
  $("form[name='registration']").validate({

    // Specify the validation rules
    rules: {
      fileToUpload: {
        required: true,
        accept: "*.PNG|*.jpe?g|*.GIF|*.png|*.JPE?G|*.gif"
      }
	 
    },
    // Specify the validation error messages
    messages: {
      fileToUpload: {
        required: "Please upload  image",
        accept: "File must be JPG, GIF or PNG"
      }
    },

    submitHandler: function(form) {
      form.submit();
    }
  });
  
});
</script>

尝试为规则中的其他格式提供* .png和相同的内容。希望这可以解决您的问题。