admin管理员组文章数量:1322838
I am trying to implemenet validation for file input type using jQuery validator plugin. The file input type should accept only images with .jpeg, .jpg & .png extension & file size should not be greater than 1MB. Validation for other input fields work without any issues. Required validation works for file input type.
What could be the issue?
Javascript
jQuery.validator.addMethod('filesize', function(value, element, param) {
return this.optional(element) || (element.files[0].size <= param)
});
$('#form').validate({
rules: {
firstname: {
minlength: 6,
required: true
},
lastname: {
minlength: 6,
required: true
},
file: {
required: true,
accept: "png|jpeg|jpg",
filesize: 1048576
}
},
messages:
{
file: "File must be JPEG or PNG, less than 1MB"
},
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
HTML
<form id="form">
<div class="form-group">
<label class="control-label" for="firstname">First:</label>
<div class="input-group">
<input class="form-control" name="firstname" type="text" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="lastname">Last:</label>
<div class="input-group">
<input class="form-control" name="lastname" type="text" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="file">Image:</label>
<div class="input-group">
<input id="file" name="file" type="file" />
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Fiddle that doesn't give desired result
I am trying to implemenet validation for file input type using jQuery validator plugin. The file input type should accept only images with .jpeg, .jpg & .png extension & file size should not be greater than 1MB. Validation for other input fields work without any issues. Required validation works for file input type.
What could be the issue?
Javascript
jQuery.validator.addMethod('filesize', function(value, element, param) {
return this.optional(element) || (element.files[0].size <= param)
});
$('#form').validate({
rules: {
firstname: {
minlength: 6,
required: true
},
lastname: {
minlength: 6,
required: true
},
file: {
required: true,
accept: "png|jpeg|jpg",
filesize: 1048576
}
},
messages:
{
file: "File must be JPEG or PNG, less than 1MB"
},
highlight: function(element) {
$(element).closest('.form-group').addClass('has-error');
},
unhighlight: function(element) {
$(element).closest('.form-group').removeClass('has-error');
},
errorElement: 'span',
errorClass: 'help-block',
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
HTML
<form id="form">
<div class="form-group">
<label class="control-label" for="firstname">First:</label>
<div class="input-group">
<input class="form-control" name="firstname" type="text" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="lastname">Last:</label>
<div class="input-group">
<input class="form-control" name="lastname" type="text" />
</div>
</div>
<div class="form-group">
<label class="control-label" for="file">Image:</label>
<div class="input-group">
<input id="file" name="file" type="file" />
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Fiddle that doesn't give desired result
Share Improve this question edited Jun 4, 2015 at 17:55 Sparky 98.8k26 gold badges202 silver badges290 bronze badges asked Jun 4, 2015 at 9:00 version 2version 2 1,0593 gold badges16 silver badges36 bronze badges 2- I get a call error in the console – mplungjan Commented Jun 4, 2015 at 9:09
-
2
"...should accept only images with .jpeg, .jpg & .png extension" ~ The
accept
rule is only for MIME types. Theextension
rule is for file extensions. You also need to include theadditional-methods.js
file for these rules. – Sparky Commented Jun 4, 2015 at 17:44
1 Answer
Reset to default 6...should accept only images with .jpeg, .jpg & .png extension
The
accept
rule is only for MIME types. Theextension
rule is for file extensions. Looks like you should be using theextension
rule instead ofaccept
. You also need to include theadditional-methods.js
file in order to use these rules.In order to have access to the file's size property, you need to set the
enctype="multipart/form-data"
attribute on the<form>
tag.
Working DEMO: http://jsfiddle/mv8w3m0c/1/
本文标签: javascriptjQuery validator not working for file input typeStack Overflow
版权声明:本文标题:javascript - jQuery validator not working for file input type - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742116971a2421512.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论