简体   繁体   English

如何强制使用文件上载字段

[英]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.

相关问题 如何在angularjs中使文件类型为强制性(必填)字段? - How to make file type mandatory(Required) field in angularjs? 使用javascript强制字段 - make a field mandatory using javascript 如何使Div元素在表单中成为可编辑和必填字段 - how to make Div element editable and mandatory field in form 如何在jquery中的if else条件中强制使用字段 - how to make a field mandatory inside if else condition in jquery 如何使Grails richui g:autocomplete字段强制 - How to make a Grails richui g:autocomplete field mandatory 如何使用JavaScript在struts2中使文件上传字段为空? - How to make a file upload field empty in struts2 using javascript? 如何使用javascript检查必填字段? - How to check the mandatory field with javascript? 如何根据另一列/同一行 HTML 表中的选择强制相邻列的输入字段? - How to make adjacent col's input field mandatory based on a selection in another column/same row HTML Table? 选中分配给它的单选按钮时,如何使输入/文本字段为必填字段? - How do I make an input/text field mandatory when the radio button assigned to it is checked? Javascript:当选中/选中分配给它的单选按钮时,如何使输入/文本字段为必需字段? - Javascript: How do I make an input/text field mandatory(required) when the radio button assigned to it is checked/selected?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM