I am not able to give validations while selecting second file in file browse button, for first file i am able to apply validations, here is my code in mvc
<div id="fileUploadContainer">
<div id="uploadContainer" style="height:30px" >
<input type="file" id="file1" name="file1" onchange="ValidateSingleInput(this);" />
</div> </div>
<a id="btnAdd" href="#">Add Another</a>
By clicking on hyperlink(Add Another) I am Calling Jquery function to add another fileupload button,The jquery function is
$('#btnAdd').click(function (e) {
e.preventDefault();
var htmlFormatDiv = $("<div id='uploadContainer' style='height:30px'></div>");
var htmlFormatFile = $("<input type='file' />");
var totalFileCount = $("#fileUploadContainer").children("div").length;
var fuData = document.getElementById('<%= file1.ClientID %>');
htmlFormatFile.attr("id", "file1");
htmlFormatFile.attr("name", "file1");
htmlFormatDiv.attr("id", "uploadContainer");
htmlFormatDiv.append(htmlFormatFile);
$("#fileUploadContainer").append(htmlFormatDiv);
});
});
Like this I am adding multiple files, now i want to give validations for the files to accept only pdf and jpg and it should not exceed 3 mb, I added one java script function to do this, but I am able to give validations to only the first file, It's not working for remaining files which are added with hyperlink(Add Button),The java script function which i used is:
<script type="text/javascript" language="javascript">
var _validFileExtensions = [".jpg", ".jpeg", ".pdf"];
function ValidateSingleInput(oInput) {
if (oInput.type == "file") {
var sFileName = oInput.value;
if (sFileName.length > 0) {
var fsize = sFileName.size;
if (fsize > 1048576)
{
alert(fsize + " bites\nToo big!");
}
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
blnValid = true;
break;
}
}
if (!blnValid) {
alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
oInput.value = "";
return false;
}
}
}
return true;
}
</script>
If any one faced this problem please help,(How to do validations for multiple files which are added with hyperlink)