I have a code but I need a javascript validation that checks maximum upload file size like check if the uploaded file is increased 1-MB he show error file is increased chosse less then 1MB file ...
I have this code how use maximum file size regular expression and whar code I use in this code that check maximum size validation.
<form action="" method="post">
<script type="text/javascript">
function ValidateExtension() {
var allowedFiles = [".doc", ".docx", ".pdf"];
var fileUpload = document.getElementById("fileUpload");
var lblError = document.getElementById("lblError");
var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$");
if (!regex.test(fileUpload.value.toLowerCase())) {
lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only.";
return false;
}
lblError.innerHTML = "";
return true;
}
</script>
<input id="fileUpload" type="file" />
<br />
<span id="lblError" style="color: red;"></span>
<br />
<input type="submit" id="btnUpload" value="Upload" onclick="return ValidateExtension()" />
</form>
I have a code but I need a javascript validation that checks maximum upload file size like check if the uploaded file is increased 1-MB he show error file is increased chosse less then 1MB file ...
I have this code how use maximum file size regular expression and whar code I use in this code that check maximum size validation.
<form action="" method="post">
<script type="text/javascript">
function ValidateExtension() {
var allowedFiles = [".doc", ".docx", ".pdf"];
var fileUpload = document.getElementById("fileUpload");
var lblError = document.getElementById("lblError");
var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$");
if (!regex.test(fileUpload.value.toLowerCase())) {
lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only.";
return false;
}
lblError.innerHTML = "";
return true;
}
</script>
<input id="fileUpload" type="file" />
<br />
<span id="lblError" style="color: red;"></span>
<br />
<input type="submit" id="btnUpload" value="Upload" onclick="return ValidateExtension()" />
</form>
Share
Improve this question
edited Nov 11, 2021 at 6:09
Art
3,0994 gold badges22 silver badges40 bronze badges
asked Nov 8, 2015 at 19:34
Arslan KhalilArslan Khalil
412 gold badges2 silver badges6 bronze badges
3
- <form action="" method="post"> <script type="text/javascript"> function ValidateExtension() { var allowedFiles = [".doc", ".docx", ".pdf"]; var fileUpload = document.getElementById("fileUpload"); var lblError = document.getElementById("lblError"); var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$"); if (!regex.test(fileUpload.value.toLowerCase())) { lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only."; return false;} lblError.innerHTML = ""; return true;} </script> – Arslan Khalil Commented Nov 8, 2015 at 19:45
- <input id="fileUpload" type="file" /> <br /> <span id="lblError" style="color: red;"></span> <br /> <input type="submit" id="btnUpload" value="Upload" onclick="return ValidateExtension()" /> </form> – Arslan Khalil Commented Nov 8, 2015 at 19:46
- This code i use how i done javascript maximum size validation in this code. – Arslan Khalil Commented Nov 8, 2015 at 19:46
1 Answer
Reset to default 8This should get you started.
function validate(el) {
var maxfilesize = 1024 * 1024, // 1 Mb
filesize = el.files[0].size,
warningel = document.getElementById( 'lbError' );
if ( filesize > maxfilesize )
{
warningel.innerHTML = "File too large: " + filesize + ". Maximum size: " + maxfilesize;
return false;
}
else
{
warningel.innerHTML = '';
return true;
}
}
.warning { font-style: italic; }
<form enctype="multipart/form-data" method="POST">
<input type='file' name='f' onchange='validate(this)'>
<div id='lbError' class='warning'></div>
<input type='submit' onsubmit='return validate()'/>
</form>