input type with size validations
input type with size validations using css and jquery
by Abhay Singh
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="main">
<div class="media">
Media
</div>
<div class="uploadsec">
<div class="file">
<span>
<span id="filename">Upload Image</span>
<i class="fa fa-photo"></i></span>
<input type="file" name="Docfile" id="Docfile" accept="image/*">
</div>
<div class="instruction">
The file can be upto 151KB
</div>
<div class="file">
<span>
<i class="fa fa-video-camera"></i></span>
<input type="url" id="videofile" placeholder="Upload video (URL)">
</div>
</div>
</div>
CSS
body {
paddinh:0;
margin:0;
}
.main {
padding:15px;
background:#fff;
overflow:hidden;
}
.media {
width:40%;
float:left;
}
.uploadsec {
float:right;
width:60%;
overflow:hidden;
}
.file {
font-size: 10px;
letter-spacing: 0.02em;
text-transform: uppercase;
color: #ffffff;
text-align: center;
width: 100%;
position: relative;
overflow: hidden;
background:#eee;
}
.file span {
background: transparent;
width: 100%;
height: 30px;
line-height: 30px;
font-family: 'myriadpro-regular';
font-size: 14px;
color: #666;
text-align: left;
text-transform: none;
padding-left: 10px;
box-sizing: border-box;
float: left;
color:#999;
}
.file i {
float: right;
width: 50px;
color: #999;
line-height: 30px;
text-align: right;
font-size: 20px;
padding-right: 5px;
box-sizing: border-box;
}
.uploadsec input[type=file], .uploadsec input[type=url] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
opacity: 0;
position: relative;
width: 100%;
height: 35px;
font-weight: 700;
font-size: 0.5em;
line-height: 28px;
letter-spacing: 0.2em;
position: absolute;
left: 0;
top: 0;
height: 100%;
z-index: 10;
}
#filename, #videoname {
width:calc(100% - 50px);
}
.instruction {
text-align:right;
padding:10px 0 10px 0;
color:#999;
}
.uploadsec input[type=url] {
color:#999;
opacity:1;
border:none;
background:transparent;
outline:none;
font-size:12px;
letter-spacing:0;;
padding-left:20px;
width:calc(100% - 50px);
}
::-webkit-input-placeholder {
color: #999;
}
::-moz-placeholder {
color: #999;
}
:-ms-input-placeholder {
color: #999;
}
.uploadsec:-moz-placeholder {
color: #999;
}
JavaScript
$('#Docfile').change(function () {
var filename = $(this).val().replace(/C:\\fakepath\\/i, '');
if(this.files[0].size > 155181){
alert("File is too big! Please upload file size less than 151kb");
this.value = "";
}
else {
$('#filename').text(filename);
}
});