Friday 22 April 2016

file type validation in jquery


Introduction:

Here I will explain how to validate file type like JPG,PNG or JPEG using Jquery or Javascript. Sometimes required this type of validation like upload only  JPG,PNG or JPEG file otherwise alert to user "file type is not valid to upload "

Description: 

In previously post I explained to  
Change the column name or datatype or size in sql server
How to set or get textbox value using Javascript or Jquery
Server does not support secure connections  
textbox allow only characters javascript
button click event not firing in asp.net c#

And now here I explain to how to validate file type like JPG,PNG or JPEG using Jquery or Javascript in asp.net.

Now copy the following javascript code in your page.



function IsFileValid() {

        var objFileUpload = document.getElementById("fuContentImage");
        if (objFileUpload.value != null || objFileUpload.value != "") {
            var file;
            file = objFileUpload.files[0];
            if ((file.name.split('.').pop().toLowerCase()) == "png" || (file.name.split('.').pop().toLowerCase()) == "jpg" || (file.name.split('.').pop().toLowerCase()) == "jpeg" || (file.name.split('.').pop().toLowerCase()) == "bmp") {

            }
            else {
                alert("Please upload png,jpg or jpeg image only.");
                return false;
            }
           
           
        }
        return true;
    }


And copy this html in your page




<h1>how to validate file type like JPG,PNG or JPEG using Jquery or Javascript</h1>

<input type="file" name="Image" id="fuContentImage" />  

<input type="submit" onclick="return IsFileValid();" value="Add" class="btn btn-success" style="width:200px" />






 


No comments:

Post a Comment



Asp.net tutorials