Skip to content

Instantly share code, notes, and snippets.

@shrekuu
Forked from siamak/form.html
Created May 11, 2017 10:27
Show Gist options
  • Save shrekuu/455bf2154600b02871696b760326fbbc to your computer and use it in GitHub Desktop.
Save shrekuu/455bf2154600b02871696b760326fbbc 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