0

I have file upload control for validating only(.xlsx|.xls) two extensions taking java script here. When i select valid extension it shows selected file name it's fine again. When i click browse button value is not clear. The value is clear when i select if invalid extension after raising the alert message click ok then only file upload control value is removing.

what i need when i select first valid file name after click again browse button value should be clear.

when i run this code in Mozilla fire fox it shows what i selected value. But in chrome it not showing invalid extension names.

My code:

<script>
    var extension = [".xlsx", ".xls"];
    function Validate(oInput) {
        if (oInput.type == "file") {
            var sFileName = oInput.value;
            if (sFileName.length > 0) {
                var blnValid = false;
                for (var j = 0; j < extension.length; j++) {
                    var sCurExtension = extension[j];
                    if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                        blnValid = true;
                        break;
                    }
                }
                if (!blnValid) {
                    alert("Sorry, invalid File, allowed extensions are: " + extension.join(", "));
                    oInput.value = "";
                    return false;
                }
            }
        }
        return true;
    }
</script>
RajeshKdev
  • 6,029
  • 6
  • 53
  • 76
Antony
  • 37
  • 8

1 Answers1

0
Use this It's work.

<script type="text/javascript">
    //<![CDATA[
        function resetFileInput()
        {
            var parent = document.getElementById('fileInputContainer'),
                newFileInput = document.createElement('input');

            parent.removeChild(document.getElementById('file'));

            newFileInput.setAttribute('type', 'file');
            newFileInput.setAttribute('id', 'file');

            parent.appendChild(newFileInput);
        }
    //]]>