user3556374
user3556374

Reputation: 1

Javascript validate filename before upload 2

Hello Friends i Want to validate upload file option for a specify filename.forexample if my file name is test.docx then user have to uplaod only test.docx from file upload option if user upload another file with different name then it will display popup error "FileName is worng"

Upvotes: 0

Views: 1268

Answers (2)

Vinz243
Vinz243

Reputation: 9938

Here is some javascript code.

$('input#file-input').on('change', function () {
    var file = $(this).val();
    if (!endsWith(file, ".docx")) {
        alert("Invalid file");
    }
});
function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

http://jsfiddle.net/y4ebE/

But remember, never trust user input. editing the script or using a cmd such as curl make possible to upload another file. Do backend validation!

Upvotes: 1

     // jQuery 
      $(document).ready(function () {
            $("input[type='file']").bind('change', function () {
                for (var i = 0; i < this.files.length; i++) {
                    // check you file name or type here
                    var name = this.files[i].name;
                    var size = this.files[i].size;
                    var type = this.files[i].type;
                    var lastModified = this.files[i].lastModifiedDate;
                }
      });

// html
<input type="file" name="file" />

Upvotes: 0

Related Questions