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