ソースを参照

#844: Add option to evaluate length in UTF-8 bytes, thanks to @thx2001r

phuoc 11 年 前
コミット
9f3f856a47

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@
 __New Features__
 
 * [#807](https://github.com/nghuuphuoc/bootstrapvalidator/issues/807), [#821](https://github.com/nghuuphuoc/bootstrapvalidator/pull/821): Add ```min```, ```max``` options for the [date](http://bootstrapvalidator.com/validators/date/) validator, thanks to [@Arkni](https://github.com/Arkni)
+* [#844](https://github.com/nghuuphuoc/bootstrapvalidator/pull/844): The [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator adds option to evaluate length in UTF-8 bytes, thanks to [@thx2001r](https://github.com/thx2001r)
 
 __Language Packages__
 

+ 1 - 0
CONTRIBUTORS.md

@@ -83,6 +83,7 @@ I would like to give big thanks to the following contributors:
 * [@stepin](https://github.com/stepin)
 * [@talberti](https://github.com/talberti)
 * [@thisisclement](https://github.com/thisisclement)
+* [@thx2001r](https://github.com/thx2001r)
 * [@tiagofontella](https://github.com/tiagofontella)
 * [@tomByrer](https://github.com/tomByrer)
 * [@trondulseth](https://github.com/trondulseth)

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.3-dev, built on 2014-09-27 8:06:39 PM
+ * @version     v0.5.3-dev, built on 2014-09-27 8:17:53 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 26 - 7
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
- * @version     v0.5.3-dev, built on 2014-09-27 8:06:39 PM
+ * @version     v0.5.3-dev, built on 2014-09-27 8:17:54 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -6056,7 +6056,8 @@ if (typeof jQuery === 'undefined') {
         html5Attributes: {
             message: 'message',
             min: 'min',
-            max: 'max'
+            max: 'max',
+            utf8bytes: 'utf8Bytes'
         },
 
         enableByHtml5: function($field) {
@@ -6081,6 +6082,7 @@ if (typeof jQuery === 'undefined') {
          * @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
@@ -6089,6 +6091,7 @@ if (typeof jQuery === 'undefined') {
          *      - 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}
          */
         validate: function(validator, $field, options) {
@@ -6097,11 +6100,27 @@ if (typeof jQuery === 'undefined') {
                 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),
+                // Credit to http://stackoverflow.com/a/23329386 (@lovasoa) for UTF-8 byte length code
+                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;
+                             },
+                length     = options.utf8Bytes ? utf8Length(value) : 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;

ファイルの差分が大きいため隠しています
+ 3 - 3
dist/js/bootstrapValidator.min.js


+ 13 - 9
src/js/validator/stringLength.js

@@ -47,8 +47,6 @@
          * - 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();
@@ -58,17 +56,23 @@
 
             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) {
+                // Credit to http://stackoverflow.com/a/23329386 (@lovasoa) for UTF-8 byte length code
+                utf8Length = function(str) {
                                  var s = str.length;
-                                 for (var i=str.length-1; i>=0; i--) {
+                                 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--;
+                                     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),
+                             },
+                length     = options.utf8Bytes ? utf8Length(value) : value.length,
                 isValid    = true,
                 message    = options.message || $.fn.bootstrapValidator.i18n.stringLength['default'];