Skip to content

Instantly share code, notes, and snippets.

@siamak
Created June 22, 2016 08:41
Show Gist options
  • Save siamak/2c6dc7a76dbd2a1fbf382b1bc3e27050 to your computer and use it in GitHub Desktop.
Save siamak/2c6dc7a76dbd2a1fbf382b1bc3e27050 to your computer and use it in GitHub Desktop.
Image Validator (Dimension) with javascript.
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h3>Image Validator: </h3>
<input type="file">
</body>
</html>
function formValidate(){
var _URL = window.URL || window.webkitURL;
var message;
var $el = $('#work_file');
var file = $el[0].files[0];
if(file) {
var image;
var imgw, imgh;
// if ((file = $('#work_file')[0].files[0])) {
image = new Image();
image.onload = function () {
console.log(this);
imgw = this.width;
imgh = this.height;
if(!/(\.zip|\.tiff|\.tif|\.jpg|\.jpeg)$/i.test(file.name)){
message.type = 'type';
return false;
} else if(file.size > 4194304) {
message.size = 'size';
return false;
} else if(imgw > 1000 || imgh > 1000) {
message.res = 'res';
return false;
} else {
return true;
}
image.src = _URL.createObjectURL(file);
};
}
}
$('input[type=file]').change(formValidate);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment