check size and extension in js (and some minor improvements)
[edumed.git] / stage2 / static / js / checkfile.js
diff --git a/stage2/static/js/checkfile.js b/stage2/static/js/checkfile.js
new file mode 100644 (file)
index 0000000..f15c134
--- /dev/null
@@ -0,0 +1,25 @@
+$(function() {
+    "use strict";
+    $('input[type=file]').on('change', function () {
+        if (window.FileReader && this.files && this.files[0]) {
+            var ok = true;
+            var name = this.files[0].name;
+            if (this.getAttribute('data-ext')) {
+                var ext = this.getAttribute('data-ext');
+                var re = new RegExp('\\.(' + ext + ')$', 'i');
+                if (!re.exec(name)) {
+                    alert('Błędne rozszerzenie! Powinno być jedno z: ' + ext.replace(/\|/g, ', '));
+                    ok = false;
+                }
+            }
+            var size = this.files[0].size;
+            if (size > 10 * 1024 * 1024) {
+                alert('Rozmiar pliku nie może przekraczać 10 MB!');
+                ok = false;
+            }
+            if (!ok) {
+                this.form.reset();
+            }
+        }
+    });
+});