Monday, March 19, 2012

Image Uploading validation using Javascript




JavaScript Function Code:- 

<script language="javascript" type="text/javascript">
            function validate() {
                var result = false;
                var upfile = document.getElementById("FileUpload1").value;
                if (upfile != "") {
                    var accept = "png,gif,jpg,jpeg,PNG,GIF,JPG,JPEG".split(',');
                    var getExtention = upfile.split('.');
                    var extention = getExtention[getExtention.length - 1];
                    for (i = 0; i < accept.length; i++) {
                        if (accept[i] == extention) {
                            result = true;
                            break;
                        }
                    }
                    if (!result) {
                        alert("allowed file extetions are png,gif,jpg,jpeg");
                    }

                }
                else {
                    alert("select image to Upload");
                }
                return result;
            }
        </script>


write this javascript function then use this function as given below code in your button or any other control as per use.

<INPUT TYPE=FILE NAME="FileUpload1"><BR>
<INPUT TYPE=SUBMIT VALUE="Submit" onClick="return validate();">    

<asp:Button ID="btnUploadImage" runat="server" Text="Upload Image" 
            OnClientClick="return validate();" onclick="btnUploadImage_Click"  />

No comments:

Post a Comment