Input type=file, restrict to .rpt only

258 views Asked by At

I'm using tag <input type="file" accept="application/x-rpt, magnus-internal/rpt"/> to only allow .rpt files to be uploaded, but it's unsuccessful. User still can upload whatever they want.

What's wrong here? Please help me. Thank you so much.

1

There are 1 answers

0
Nikhil Rathod On

Put the following script in head

<script type="text/javascript">
    function ValidateForm() {
        var fileBox = document.getElementById("fileBox");
        var val = fileBox.value;
        var splittedValue = val.split(".");
        //alert(splittedValue.length);

        //for (var i = 0; i < splittedValue.length; i++) {
        //    alert(splittedValue[i]);
        //}

        var NthElementIndex = splittedValue.length - 1;
        var nThElement = splittedValue[NthElementIndex];

        if (nThElement != "jpg"
            && nThElement != "rpt") {
            alert("Please select valid rpt file");
        }
    }
</script>

Now Use the following information for id in input tag

<input type="file" id="fileBox" />
<input type="button" onclick="ValidateForm()" value="Validate" />