-
-
Save shrekuu/455bf2154600b02871696b760326fbbc to your computer and use it in GitHub Desktop.
Image Validator (Dimension) with javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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