Validating File Size Before Form Submit (HTML5)

Written by Saran on October 9, 2013, Updated October 12, 2018

Earlier browsers did not support HTML5 File API, so normally people would use flash or Java Applets to validate file format and sizes in HTML upload forms, and most of us preferred to skip the client part letting server handle the problem.
But in recent years, the HTML5 is doing wonders, it comes with many features including the client-side file objects manipulation (File API). Which means no more 3rd party plug-ins such as flash or Java applets.

Browser Support

File Size Validation (Client-side)

Here's what I did to an upload form that uses jQuery.
When the button is clicked, the Javascript checks whether the client browser supports this feature called File API or not, and then we move to next part where we actually access the size from selected file and decide our next move.

Demo :

Conclusion

The above examples shows the basic things you can do with File API, directly interact with client files without the use of alternative methods. All modern browsers should support HTML5's File API, you can check caniuse.com for any changes. Good luck!

About Sanwebe.com

Sanwebe.com was founded in 2011 by Saran Chamling. A blog truely inspired by the ever changing web development world. It is a small effort to provide useful tips, tutorials and related resources to web developers.