[英]How to validate uploaded file with jquery plugin?
I am using this script in wordpress. 我在wordpress中使用此脚本。
Here is my code: 这是我的代码:
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://raw.github.com/jzaefferer/jquery-validation/master/jquery.validate.js"></script>
<script src="https://raw.github.com/jzaefferer/jquery-validation/master/additional-methods.js"></script>
<script>
jQuery(document).ready(function($){
$("#subsciptionForm").validate({
rules: {
headshot: {
required: true,
accept: "jpg,png,jpeg,gif"
},
actorcv: {
required: true,
accept: "application/msword, application/pdf"
}
},
messages: {
headshot: {
required: 'Select an image to upload',
accept: 'Only images with type jpg/png/jpeg/gif are allowed'
},
actorcv: {
required: "Select a CV",
accept: 'Only doc/docx/pdf files are allowed'
}
},
submitHandler: function(form) {
//form.submit();
var url = '<?php echo SET_SUBSCRIBER; ?>';
var datastring = $("form").serialize();
alert(datastring); return false;
$.ajax({
type: "POST",
url: url,
data: datastring,
success: function(data) {
//alert(data); return false;
form.submit();
}
});
return false;
}
});
});
</script>
Here is the form field 这是表格栏位
<!-- Upload Headshot -->
<tr>
<td class="title_cell" width="23%">
Upload Headshot :<span class="required">*</span>
</td>
<td class="field_cell">
<input type="file" class="required" name="headshot" size="25"> (jpg, gif or png only, with maximum 1MB size)
</td>
</tr>
<!-- Upload Actor's CV -->
<tr>
<td class="title_cell" width="23%">
Upload Actor's CV :<span class="required">*</span>
</td>
<td class="field_cell">
<input type="file" class="required" name="actorcv" size="25"> (MS-word or PDF only, with maximum 1MB size)
</td>
</tr>
It works well with for image file validation but does not validate pdf and doc file. 它可以很好地用于图像文件验证,但不能验证pdf和doc文件。 Keep on giving the same message i have defined in message " Only doc/docx/pdf files are allowed
". 继续给出我在消息“ Only doc/docx/pdf files are allowed
”中定义的消息。 Also i get this in console : 我也在控制台中得到了这个:
TypeError: b.browser is undefined
Edited : 编辑:
The TypeError is gone after Kevin B help in comment but it is still not validating pdf file. 在Kevin B的注释帮助下,TypeError消失了,但它仍然无法验证pdf文件。 Any idea ? 任何想法 ?
The accept
rule is for validating by mime-type . accept
规则用于通过mime-type进行验证 。
If you're trying to validate by file extension, you'd want to use the extension
rule. 如果您尝试通过文件扩展名进行验证,则需要使用extension
规则。
See: http://docs.jquery.com/Plugins/Validation/CustomMethods/extension#extension 请参阅: http : //docs.jquery.com/Plugins/Validation/CustomMethods/extension#extension
rules: {
headshot: {
required: true,
extension: "jpg,png,jpeg,gif"
},
actorcv: {
required: true,
accept: "application/msword, application/pdf"
}
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.