27 March 2013

How to add File Size Validation through Jquery?

Few days ago, I was struggling to add this validation on my website, and after a long search , I was successful in implementing this, Using the following simple script you can add this validation very easily

<script type = "text/javascript">
jQuery("input[type=file]").live("change", function(e){
e.preventDefault();
var file_size = jQuery(this)[0];
if(file_size.files[0].size > 2000000){
alert("File Size should be  Maximum of 2 mb");
return false;
}
});
</script>


The above Script is simply adding a change event on file input type and check for Size of file input type and accordingly show alert message to user

I hope this simple script will help you, If any queries feel free to ask

No comments:

Post a Comment