File Type validation using Javascript

JavaScript jQuery Programming Technology

Today, We want to share with you File Type validation using Javascript.In this post we will show you JavaScript validation to check input file type(extension), hear for Validate File extension before Upload using Regular Expression we will give you demo and example for implement.In this post, we will learn about How to validate upload file size and file extension using JavaScript with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Snippet Contents

File Type validation using Javascript

There are the Following The simple About File Type validation using Javascript Full Information With Example and source code.

As I will cover this Post with live Working example to develop Javascript file size and extension validation before upload, so the javascript validation for file upload extension and size for this example is following below.

Step 1: JavaScript Code

function checkFileExte(){
    var uploadedFiles = document.getElementById('file');
    var liveimgPath = uploadedFiles.value;
    var allowedExtensions = /(\.jpg|\.jpeg|\.png|\.gif)$/i;
    if(!allowedExtensions.exec(liveimgPath)){
        alert('Please upload file having extensions .jpeg/.jpg/.png/.gif only.');
        uploadedFiles.value = '';
        return false;
    }else{
        //Image validating preview
        if (uploadedFiles.files && uploadedFiles.files[0]) {
            var reader = new FileReader();
            reader.onload = function(e) {
                document.getElementById('displayImg').innerHTML = '<img src="'+e.target.result+'"/>';
            };
            reader.readAsDataURL(uploadedFiles.files[0]);
        }
    }
}

Step 2: HTML Code

<input type="file" id="file" onchange="return checkFileExte()"/>

<div id="displayImg"></div>

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about File Type validation using Javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   JavaScript GROUP BY Function with Example
  2.   Get Facebook Access Token Using PHP SDK - Authentication
  3.   PHP cURL Basic CRUD Example
  4.   jQuery AJAX laravel 6 Delete Data from database
  5.   Multiple file upload extension validation using jQuery Validate
  6.   PHP Codeigniter 3 Multiple WHERE conditions Example
  7.   10 little Importance PHP functions list with Example
  8.   html5 range slider with 2 handles using jQuery
  9.   Vue JS Axios Post Request using Laravel Example
  10.   Laravel Resize Image Before Upload Example

Leave a Reply

Your email address will not be published. Required fields are marked *