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.
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>
0 Comments