luci-base: validation.js: count byte- instead of character length of strings
authorJo-Philipp Wich <jo@mein.io>
Sun, 17 May 2020 19:34:30 +0000 (21:34 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 17 May 2020 19:40:27 +0000 (21:40 +0200)
Fixes: #4055
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-base/htdocs/luci-static/resources/validation.js

index eea837d64ee5c77a53c711ac31035d132f7ab4da..769d2a955376093fa5f4f72c965e4ef3bb9a8601 100644 (file)
@@ -1,6 +1,10 @@
 'use strict';
 'require baseclass';
 
+function bytelen(x) {
+       return new Blob([x]).size;
+}
+
 var Validator = baseclass.extend({
        __name__: 'Validation',
 
@@ -421,24 +425,23 @@ var ValidatorFactory = baseclass.extend({
                },
 
                length: function(len) {
-                       var val = '' + this.value;
-                       return this.assert(val.length == +len,
+                       return this.assert(bytelen(this.value) == +len,
                                _('value with %d characters').format(len));
                },
 
                rangelength: function(min, max) {
-                       var val = '' + this.value;
-                       return this.assert((val.length >= +min) && (val.length <= +max),
+                       var len = bytelen(this.value);
+                       return this.assert((len >= +min) && (len <= +max),
                                _('value between %d and %d characters').format(min, max));
                },
 
                minlength: function(min) {
-                       return this.assert((''+this.value).length >= +min,
+                       return this.assert(bytelen(this.value) >= +min,
                                _('value with at least %d characters').format(min));
                },
 
                maxlength: function(max) {
-                       return this.assert((''+this.value).length <= +max,
+                       return this.assert(bytelen(this.value) <= +max,
                                _('value with at most %d characters').format(max));
                },