Javascript validation to insert file of specific name

Viewed 1897

I want a validation in Javascript that, a file name should be of specific type should be uploaded like for an example.

I have a fileupload control which has the feature to upload multipe file.

here is the html of that.

<asp:FileUpload runat="server" ID="flufileUpload" AllowMultiple="true" onchange="return ValidateFileType(this);" />

So while uploading i want user to upload file of specific name

like:-

A user can upload only 5 files with only name as

1 -> 120.jpg

2 -> 150.jpg

3 -> 180.jpg

4 -> 210.jpg

5 -> 240.jpg

if user uploads filename other than this then it should prompt alert message.

my function for handling some validations is below. but I am unable to handle the filename validation.

function ValidateFileType(controlName) {
    try {
        var uploadControl = controlName;
        var isCValidFile = false;

        for (var i = 0; i < uploadControl.files.length; i++) {
            isCValidFile = false;

            var path = uploadControl.files[i].name;
            var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();

            if (path != "") {
                for (var j = 0; j < ValidImageFileExtension.length; j++) {
                    if (ext == ValidImageFileExtension[j]) {
                        isCValidFile = true;
                        break;
                    }
                }
                if (!isCValidFile) {
                    jAlert("Invalid File. Please upload a File with extension " + ValidImageFileExtension.join(", "), "Information");
                    uploadControl.value = '';
                    isCValidFile = false;
                    break;
                }
                else if (uploadControl.files[i].size > 15728640) {
                    jAlert("Please check file size should be less than 15 MB.", "Information");
                    uploadControl.value = '';
                    isCValidFile = false;
                    break;
                }
            }
            else {
                jAlert("Please select File", "Information");
                isCValidFile = false;
                break;
            }
        }

        return isCValidFile;

    } catch (e) {
        isCValidFile = false;
        jAlert("Error occurred on validate file.", "Error");
        return isCValidFile;
    }
}
4 Answers
Related