My goal is to first validate the upload file is an image and if it is an image I will display it. I get the codes from Validation Code and Image Code.
At first I was able to display image. However, when I combined display image code with validation code, I did not able to get both validation and display worked.
Can anyone please help me? Below are my codes. Thanks in advance! :)
<input type="file" name="dataFile" id="fileChooser" onchange="readURL(this);" />
<SCRIPT type="text/javascript">
var _validFileExtensions = [".jpg", ".jpeg", ".bmp", ".gif", ".png"];
     function readURL(input) {
         var arrInputs = document.getElementById("fileChooser").value;
            for (var i = 0; i < arrInputs.length; i++) {
                var oInput = arrInputs[i];
                if (oInput.type == "file") {
                    var sFileName = oInput.value;
                if (sFileName.length > 0) {
                    var blnValid = false;
                    for (var j = 0; j < _validFileExtensions.length; j++) {
                        var sCurExtension = _validFileExtensions[j];
                        if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
                            blnValid = true;
                            if (input.files && input.files[0])  {
                                var reader = new FileReader();
                                reader.onload = function (e) {
                                    $('#blah').attr('src', e.target.result)
                                };
                                reader.readAsDataURL(input.files[0]);
                            }
                            break;
                        }
                    }
                    if (!blnValid) {
                        alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
     
     
    