$(".postreplyForm").submit(function(event) {
var fileBox = $('.post-file').val();
if (fileBox === '') {
return false; //prevent form to submit
} else {
//File format
var fileExtension = ['jpg', 'jpeg', 'png', 'mp3', 'mp4', 'pdf', 'pps', 'ppt', 'pptx', 'xls', 'doc', 'docx', 'txt'];
var flag = true; //decalre this
var fileArray = $('.post-file')[0].files; //get all files
$(".errorFile").html('');
//loop through files
$.each(fileArray, function(i, v) {
name = v.name; //name of file
fileSize = v.size; //size of file
console.log(name);
if ($.inArray(name.split('.').pop().toLowerCase(), fileExtension) == -1) {
flag = false;
$(".errorFile").html('<br><center><p class="signuperror">File format not allowed.</p></center><br>');
} else {
if (fileSize > 1000000) {
$(".errorFile").html('<br><center><p class="signuperror">The file is too big.</p></center><br>');
flag = false;
}
}
})
}
//check not true prevnt submit
if (!flag) {
return false;
} else {
return true;
}
});
.errorFile {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="create-mode postreplyForm" method="post" action="a.php" name="post" enctype="multipart/form-data">
<table class="table-newpost">
<tr>
<td colspan="5"><br>
<div style="font-weight: bold; font-size: 14px; margin-bottom: -10px;">Attach file</div>
<br>
<span>File formats allowed: (.jpg, .jpeg, .png, .mp3, .mp4, .pdf, .zip, .rar, .pps, .ppt, .pptx, .xls, .doc, .docx, .txt, .rtf)</span>
<br><br>
<div class="fileBox">
<div style="display: flex; margin-bottom: 2px;">
<input type="file" name="file[]" id="fileID1" class="post-file" multiple="multiple">
<div id="1" class="fileBtn fileDeleteInPost" title="Delete file">x</div>
<div class="fileBtn add-file" title="Add file">+</div>
</div>
</div>
<div class="errorFile"></div>
</td>
</tr>
<tr>
<td colspan="5" style="text-align: right;">
<input type="hidden" name="reply-by" value="<?php echo $_SESSION['userId']; ?>">
<button type="submit" name="submit-postreply" id="Miau">Submit</button>
</td>
</tr>
</table>
</form>