-
-
Save n-ramdi/4716fe634749cbfeddbd11c28170654c to your computer and use it in GitHub Desktop.
Magento file size and file extensions frontend validation
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
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