Validate File size While Upload with the help of Javascript | Sanwebcorner

Validate File size While Upload with the help of Javascript

This post shows how to validate the file while upload the data. While uploading the data it checks the validation like the file size should be less then 2MB and you also validate  the format of the file. Here i checking the file size should be less then 2MB. If the size should be more than 2mb it shows the error "File size is grater than 2mb.Please upload the file below 2mb." Like that.

This is the script to validate the file while upload:

<script>
function validate() {
$("#file_error").html("");
$(".inputbox1").css("border-color","#F0F0F0");
var file_size = $('#file')[0].files[0].size;
if(file_size>2097152) {
$("#file_error").html("File size is greater than 2MB. Please upload file below 2MB.");
$(".inputbox1").css("border-color","#FF0000");
return false;
return true;
}
</script>

It showing red border in the text box and given the error message below the text box  when validation error occurs. Please Check the demo page to check this example. This is one of the very useful concept for the programmers. I hope this is very useful.

Code for Validate File on Upload:

<html>
<head>
<style>
#filesizeval {width:500px; margin:0 auto;    border: #050505 2px solid;
background: #F1F0F0;padding:80px; }
.inputbox1{
padding:10px;    
margin: 0 auto;display: 
block;     
border: 1px solid #A39F9F; 
border-radius:4px;
background-color:#FFF;
}
#file_error
{
color: #FF0000;
}
#button
{
background-color:#2FC332;
margin: 23px auto;
display: block; 
border:0;
padding:10px 40px; 
color:#FFF;
border:#F0F0F0 1px solid; 
border-radius:4px;
}
</style>
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
<script>
function validate() {
$("#file_error").html("");
$(".inputbox1").css("border-color","#F0F0F0");
var file_size = $('#file')[0].files[0].size;
if(file_size>2097152) {
$("#file_error").html("File size is greater than 2MB. Please upload file below 2MB.");
$(".inputbox1").css("border-color","#FF0000");
return false;
return true;
}
</script>
</head>
<body>
<form name="filesizeval" id="filesizeval" method="post" action=""  onSubmit="return validate();">
<div><input type="file" name="file" id="file" class="inputbox1" /> <br/><center><span id="file_error"></span></center></div>
<div><input type="submit" id="button" value="Upload"/></div><br/><br/>
<center><a style="color: #000; font-weight: bold; font-size: 17px;" href="http://www.sanwebcorner.com">www.sanwebcorner.com</a></center>
</form>
</body>
</html>

Post a Comment

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form Multi-Step-form mysql News ticker on hover change text to image our team page password generator password strength checker Photoshop PHP populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips Youtube