Browse Source

Added option to evaluate length in UTF-8 bytes

Set the utf8Bytes boolean to "true" (defaults to "false") to compare the min/max string length in UTF-8 bytes to facilitate validation for database inserts. By setting the boolean as "false" or not specifying it, is compatible with existing functionality of this validator so this shouldn't affect anyone's existing usage of this validator.

This code is based on that by lovasoa (http://github.com/lovasoa) posted at the following web address on 4/27/2014: http://stackoverflow.com/a/23329386
Joseph Reiter 11 years ago
parent
commit
641a8b64bf
1 changed files with 21 additions and 6 deletions
  1. 21 6
      src/js/validator/stringLength.js

+ 21 - 6
src/js/validator/stringLength.js

@@ -10,7 +10,8 @@
         html5Attributes: {
             message: 'message',
             min: 'min',
-            max: 'max'
+            max: 'max',
+            utf8bytes: 'utf8Bytes'
         },
 
         enableByHtml5: function($field) {
@@ -35,6 +36,7 @@
          * @param {Object} options Consists of following keys:
          * - min
          * - max
+         * - utf8Bytes
          * At least one of two keys is required
          * The min, max keys define the number which the field value compares to. min, max can be
          *      - A number
@@ -43,7 +45,10 @@
          *      - A callback function that returns the number
          *
          * - message: The invalid message
+         * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false (no changes to existing scripts)
          * @returns {Object}
+         * 
+         * Credit to http://stackoverflow.com/a/23329386 (http://github.com/lovasoa) for UTF-8 byte length code
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
@@ -51,11 +56,21 @@
                 return true;
             }
 
-            var min     = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min),
-                max     = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max),
-                length  = value.length,
-                isValid = true,
-                message = options.message || $.fn.bootstrapValidator.i18n.stringLength['default'];
+            var min        = $.isNumeric(options.min) ? options.min : validator.getDynamicOption($field, options.min),
+                max        = $.isNumeric(options.max) ? options.max : validator.getDynamicOption($field, options.max),
+                utf8length = function(str) {
+                                 var s = str.length;
+                                 for (var i=str.length-1; i>=0; i--) {
+                                     var code = str.charCodeAt(i);
+                                     if (code > 0x7f && code <= 0x7ff) s++;
+                                     else if (code > 0x7ff && code <= 0xffff) s+=2;
+                                     if (code >= 0xDC00 && code <= 0xDFFF) i--;
+                                 }
+                                 return s;
+                             }(value),
+                length     = (options.utf8Bytes ? utf8length : value.length),
+                isValid    = true,
+                message    = options.message || $.fn.bootstrapValidator.i18n.stringLength['default'];
 
             if ((min && length < parseInt(min, 10)) || (max && length > parseInt(max, 10))) {
                 isValid = false;