check file extension and alert user if isn 27t image file js

Solutions on MaxInterview for check file extension and alert user if isn 27t image file js by the best coders in the world

showing results for - "check file extension and alert user if isn 27t image file js"
Thibaut
11 May 2017
1function fileValidation(){
2    var fileInput = document.getElementById('file');
3    var filePath = fileInput.value;
4    var allowedExtensions = /(\.jpg|\.jpeg|\.png|\.gif)$/i;
5    if(!allowedExtensions.exec(filePath)){
6        alert('Please upload file having extensions .jpeg/.jpg/.png/.gif only.');
7        fileInput.value = '';
8        return false;
9    }else{
10        //Image preview
11        if (fileInput.files && fileInput.files[0]) {
12            var reader = new FileReader();
13            reader.onload = function(e) {
14                document.getElementById('imagePreview').innerHTML = '<img src="'+e.target.result+'"/>';
15            };
16            reader.readAsDataURL(fileInput.files[0]);
17        }
18    }
19}
20