Click here to Skip to main content
15,889,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

XML
<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

XML
$('#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:

C#
<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) //do something if file size more than 1 mb (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)
Posted
Updated 10-Nov-15 21:41pm
v2

1 solution

here is the solution:
http://codepen.io/xszaboj/pen/qOQKjd?editors=101[^]

problem is that you are adding:

JavaScript
var htmlFormatFile = $("<input type='file' />")


so there is no onchange function ;)

JavaScript
var htmlFormatFile = $("<input type='file' onchange='ValidateSingleInput(this);' />");



another advice:
don't add items with same id:
JavaScript
var htmlFormatDiv = $("<div id="uploadContainer" style="height:30px"></div>");


id should be unique for whole page. use class instead or data attribute if you need to store some data on you element


on more thing:
have you tried to use accept on input file element like this? It might be easier than all that javasript code:

HTML
<input type="file" accept="application/pdf,image/jpeg">
 
Share this answer
 
v3
Comments
Member 12078709 11-Nov-15 6:28am    
Thank you its working, i tried with accept but it's not working, the solution which you suggested is working :)
xszaboj 11-Nov-15 7:47am    
No problem, glad that it helped

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900