what i am trying to do in my below code i want to check one more condition when file is upload its should be check file size is greater than 10 mb then is show success message and file size is less than 10 mb then its show error message.while uploading the file .
in my below code right now what is this i check two condition when file is upload its should check jpeg or png then its show success message and file extension is not jpeg or png then its show error message.so in my below i want to check one more condition file size how can we do that.
is there any help?
function fileValidation() {
var fileInput =
document.getElementById('file');
var filePath = fileInput.value;
// Allowing file type
var allowedExtensions =
/(\.doc|\.PNG|\.jgeg)$/i;
if (!allowedExtensions.exec(filePath)) {
// alert('Invalid file type');
var y = document.getElementById("msg").innerHTML = "choose another file";
fileInput.value = '';
return false;
} else {
// alert('file type');
var y = document.getElementById("msg").innerHTML = "SUCCESS";
return true;
}
}
function tog(v) {
return v ? "addClass" : "removeClass";
}
$(document).on("input", ".clearable", function() {
$(this)[tog(this.value)]("x");
}).on("mousemove", ".x", function(e) {
$(this)[tog(this.offsetWidth - 18 < e.clientX - this.getBoundingClientRect().left)]("onX");
}).on("touchstart click", ".onX", function(ev) {
ev.preventDefault();
$(this).removeClass("x onX").val("").change();
});
.clearable {
background: #fff url(https://i.stack.imgur.com/mJotv.gif) no-repeat right -10px center;
border: 1px solid #999;
padding: 3px 18px 3px 4px;
border-radius: 3px;
transition: background 0.4s;
}
.clearable.x {
background-position: right 5px center;
}
.clearable.onX {
cursor: pointer;
}
.clearable::-ms-clear {
display: none;
width: 0;
height: 0;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<input class="clearable" type="file" id="file" onchange="return fileValidation()" />
<p id="msg"></p>
any help on this its very thankful.