Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is it possible to validate the size and type of input=file in HTML5?
Yes, it is possible to validate the size and type of input type = “file”. Use jQuery to get the desired result −
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form >
<input type="file" id="myfile" data-max-size="32154" />
<input type="submit"/>
</form>
<script>
$(function(){
$('form').submit(function(){
var val = true;
$('input[type=file][data-max-size]').each(function(){
if(typeof this.files[0] !== 'undefined'){
var max = parseInt($(this).attr('max-size'),10),
mySize = this.files[0].size;
val = max > mySize;
return val;
}
});
return val;
});
});
</script>
</body>
</html>Advertisements