How to make file upload field mandatory
javascript, jquery, validation
Solution
If you need pure Javascript: Demo Fiddle
function validate(){
var inp = document.getElementById('upload');
if(inp.files.length === 0){
alert("Attachment Required");
inp.focus();
return false;
}
}
jQuery:
function validate(){
if($('#upload')[0].files.length === 0){
alert("Attachment Required");
$('#upload').focus();
return false;
}
}
Problem
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. 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. the name of my input type has to be file[] only.