Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello every one,
I got stuck in java script validation in product adding form.In that page i have file upload control to upload product image.I am not getting how to validate that using java script.If image is not uploaded to that control i want to display "Upload Image" message in Label.How to accomplish this? Please help me.
The script i have written is
"
C#
var fileup = document.getElementById('<%=FileUploadImg.ClientID %>').value;
        if (fileup == "") {
            document.getElementById("lblFileUploadImg").innerHTML = "<font color='red'>Upload Image File</font>";
             document.getElementById('<%=FileUploadImg.ClientID %>').focus();
            return false;
        } else {
            document.getElementById("lblFileUploadImg").innerHTML = "";
        }
"
The control i have used is
" <asp:FileUpload ID="FileUploadImg" runat="server" Width="217px" Height="20px" />
<asp:Label ID="lblFileUploadImg" runat="server" > "

Please help me.Thanks in advance.
Posted
Comments
shahed.sohail 24-Mar-14 6:27am    
With jQuery you could simply do this:

$('#myFile').bind('change', function() {
if(this.files[0].size>...){
alert('File is too big');
};

});


maybe this is what you are looking for:

$("input:file").change(function () {
if ($(this).val() !== "") {
var ul_file = $(this).val();
var extension = ul_file.substr((ul_file.lastIndexOf('.') + 1));
var accepted_file_endings = ["jpg", "jpeg", "bmp", "gif", "tif", "png"];
extension = extension.toLowerCase()
if ($.inArray(extension, accepted_file_endings) !== -1) {
...

You can get validate file uploading using JavaScript like this.
ASP.NET
<script type="text/javascript">
        function validate() {
            var uploadcontrol = document.getElementById('<%=FileUploadImg.ClientID%>').value;

            //Regular Expression for fileupload control.
            var reg = /(.doc|.docx|.pdf)$/i;

            if (uploadcontrol.length > 0)
            {
                //Checks with the control value.
                if (reg.test(uploadcontrol))
                {
                    document.getElementById('<%=lblFileUploadImg.ClientID%>').innerHTML = "<font color="green">Upload Image File</font>";
                    return true;
                }
                else 
                {
                    //If the condition not satisfied shows error message.
                    document.getElementById('<%=lblFileUploadImg.ClientID%>').innerHTML = "Error while upload image";
                    return false;
                }
            }
        } //End of function validate.
    </script>


<asp:fileupload id="FileUploadImg" runat="server" width="217px" height="20px" xmlns:asp="#unknown" />
<asp:label id="lblFileUploadImg" runat="server" xmlns:asp="#unknown" />
<asp:button runat="server" text="Upload" id="btnupload" onclientclick="return validate();" xmlns:asp="#unknown" />

U can try with JQuery as well.
 
Share this answer
 
v2
 
Share this answer
 
You have an option to use, ASP .Net RequiredFieldValidator control for file upload control. Easy method ;)
 
Share this answer
 

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