Saturday, June 7, 2014

Image type validation using Javascript

Image type validation using Javascript




JQuery Function

<script type="text/javascript">
    function Validate() {
        var img = document.getElementById('image');
        var Path = img.value;
      
        if (Path == '') {
            alert("Please upload an image");

        } else {
            var Extension = Path.substring(
                    Path.lastIndexOf('.') + 1).toLowerCase();
                   
if (Extension == "gif" || Extension == "png" || Extension == "bmp"
                    || Extension == "jpeg" || Extension == "jpg") {
                if (img.files && img.files[0]) {
                    var reader = new FileReader();

                    reader.onload = function(e) {
                        $('#blah').attr('src', e.target.result);
                    }

                    reader.readAsDataURL(img.files[0]);
                }

            }
else {
                alert("Upload only GIF, PNG, JPG, JPEG and BMP images.");

            }
        }
    }
</script>

HTML

<input type="file" name="imagename" id="image" onchange="return Validate()" />


Please share your comments and feedback.Thanks.Please subscribe my updates via email.

No comments:

Post a Comment