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.
No need of JavaScript validation you can use "required" attribute in input type.... The HTML5 required attribute marks any input/textarea as being required to have a value before the form can be submitted.
Required attribute: If you want to make an input mandatory to be entered by the user, you can use the required attribute. This attribute can be used with any input type such as email, URL, text, file, password, checkbox, radio, etc. This can help to make any input field mandatory.
File Extension Validation This is basically where you are checking the extension of the presented file and making sure it is one that you should be accepting. This check is non-trivial, as there are multiple ways to evade this filtering depending on how you are implementing it.
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;
}
}
No need of JavaScript validation you can use "required" attribute in input type....
<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:
if($("#uploadField").val() != ""){
//your success code here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With