Image validations for file upload using java script

In this article , we are validate the image extension for file upload using java script , java script is a client side scripting language that means we can validate the image extension in client side.



Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="imageuploader.aspx.cs" Inherits="imageuploader" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function ValidateFileUpload() {
            var fileupload = document.getElementById('FileUpload1');
            var FileUploadPath = fileupload.value;
            if (FileUploadPath == '') {
                alert("Please upload an image");

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

                        reader.onload = function (e) {
                            $('#blah').attr('src', e.target.result);
                        }
                        reader.readAsDataURL(fileupload.files[0]);
                    }
                }
                else {
                    alert("Image  allows only GIF, PNG, JPG, JPEG and BMP. ");

                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <fieldset style="width: 350px;">
            <legend>How to validate image using file upload</legend>
            <asp:FileUpload ID="FileUpload1" runat="server" onchange="return ValidateFileUpload()" />
        </fieldset>
    </div>
    </form>

</body>
</html>

Out-put:-




 For more:-Google translator in asp.net c# ,Canvas (Dynamic) Signature using jQuery How to set textbox take only numeric using java script.,Image validations for file upload using regular expression validator in asp.net

Image validations for file upload using java script Image validations for file upload using java script Reviewed by NEERAJ SRIVASTAVA on 10:56:00 AM Rating: 5

No comments:

Powered by Blogger.