Friday, 3 November 2017

File Upload Validation Using JavaScript

 <asp:HiddenField ID="hfFlag" runat="server" Value="0" ClientIDMode="Static" />
<asp:FileUpload ID="fuImages1" onchange="ValidateFileUpload(this);" runat="server"
                                                                        ClientIDMode="Static" Width="100%" />


function ValidateFileUpload(FileUploadPath) {
            if (FileUploadPath.value == '' || FileUploadPath.value == null) {
                FileUploadPath.focus();
                document.getElementById('hfFlag').value = "0";
                alert("Please upload an image");
                return false;
            }
            else {
                var Extension = FileUploadPath.value.substring(FileUploadPath.value.lastIndexOf('.') + 1).toLowerCase();

                if (Extension == "gif" || Extension == "png" || Extension == "bmp" || Extension == "jpeg" || Extension == "jpg") {
                    document.getElementById('hfFlag').value = "1";
                    return true;
                }
                else {
                    alert("Only allows file types : GIF, PNG, JPG, JPEG and BMP");
                    document.getElementById('hfFlag').value = "0";
                    FileUploadPath.focus();
                    return false;
                }
            }
        }

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...