Skip to content

Instantly share code, notes, and snippets.

@n-ramdi
Forked from obukhow/gist:5040981
Created September 11, 2020 08:39
Show Gist options
  • Save n-ramdi/4716fe634749cbfeddbd11c28170654c to your computer and use it in GitHub Desktop.
Save n-ramdi/4716fe634749cbfeddbd11c28170654c to your computer and use it in GitHub Desktop.
Magento file size and file extensions frontend validation
Validation.addAllThese([
['validate-filesize','The file size should not exceed 100 kb.',function(v, elm){
var maxSize = 102400;
if (navigator.appName == "Microsoft Internet Explorer") {
if (elm.value) {
var oas = new ActiveXObject("Scripting.FileSystemObject");
var e = oas.getFile(elm.value);
var size = e.size;
}
} else {
if (elm.files[0] != undefined) {
size = elm.files[0].size;
}
}
if (size != undefined && size > maxSize) {
return false;
}
return true;
}],
['validate-fileextensions','Disallowed file type.',function(v, elm){
var extensions = ['jpeg','gif'];
if (!v) {
return true;
}
with (elm) {
var ext = value.substring(value.lastIndexOf('.') + 1);
for (i=0; i < extensions.length; i++) {
if (ext == extensions[i]) {
return true;
}
}
}
return false;
}],
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment