[英]How to make file upload field mandatory
Hello can anyone plz fix this problem. 你好,任何人都可以解决这个问题。
my input field is below code and I am doing validation on form submit i.eis: 我的输入字段在代码下面,我正在对表单提交进行验证,即:
<input name="file[]" type="file" multiple="multiple">
<form name="bdmrequest" enctype="multipart/form-data" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" onsubmit="return(validate());">
i have included validate.js in header and i am able to do validation of other fields except file upload. 我已在头文件中包含validate.js,我可以对文件上传以外的其他字段进行验证。 validation code i am using:
我正在使用的验证码:
if(document.bdmrequest.file.value== "")
{
alert("Attachment Required");
document.bdmrequest.file.focus();
return false;
}
js doesn't support me to use (document.bdmrequest.file[].value== "") please suggest me the alternatives. js不支持我使用(document.bdmrequest.file [] .value ==“”)请建议我替代方案。 the name of my input type has to be file[] only.
我的输入类型的名称只能是file []。
If you need pure Javascript: Demo Fiddle 如果你需要纯Javascript: Demo Fiddle
function validate(){
var inp = document.getElementById('upload');
if(inp.files.length === 0){
alert("Attachment Required");
inp.focus();
return false;
}
}
jQuery: jQuery的:
function validate(){
if($('#upload')[0].files.length === 0){
alert("Attachment Required");
$('#upload').focus();
return false;
}
}
不需要JavaScript验证,您可以在输入类型中使用“required”属性....
<input type="file" required="required">
You can check if the value is empty. 您可以检查值是否为空。
<input name="file[]" type="file" multiple="multiple" id="uploadField">
And then in js: 然后在js中:
if($("#uploadField").val() != ""){
//your success code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.