|
|
@@ -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;
|