Browse Source

#960: Add trim option for the stringLength validator

Phuoc Nguyen 11 years ago
parent
commit
b14d035159

+ 1 - 0
CHANGELOG.md

@@ -6,6 +6,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)
 * [#822](https://github.com/nghuuphuoc/bootstrapvalidator/pull/822): Add color validator, thanks to [@emilchristensen](https://github.com/emilchristensen)
 * [#844](https://github.com/nghuuphuoc/bootstrapvalidator/pull/844), [#874](https://github.com/nghuuphuoc/bootstrapvalidator/pull/874): The [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator adds option to evaluate length in UTF-8 bytes, thanks to [@thx2001r](https://github.com/thx2001r)
+* [#960](https://github.com/nghuuphuoc/bootstrapvalidator/issues/960): Add ```trim``` option for the [stringLength](http://bootstrapvalidator.com/validators/stringLength/) validator
 
 __Bug Fixes__
 * [#933](https://github.com/nghuuphuoc/bootstrapvalidator/issues/933), [#959](https://github.com/nghuuphuoc/bootstrapvalidator/issues/959): Tooltip/popover isn't destroyed when the field is valid

+ 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-10-20 3:33:28 PM
+ * @version     v0.5.3-dev, built on 2014-10-20 3:51:09 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT

+ 7 - 1
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-10-20 3:33:28 PM
+ * @version     v0.5.3-dev, built on 2014-10-20 3:51:09 PM
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
@@ -6186,6 +6186,7 @@ if (typeof jQuery === 'undefined') {
             message: 'message',
             min: 'min',
             max: 'max',
+            trim: 'trim',
             utf8bytes: 'utf8Bytes'
         },
 
@@ -6219,11 +6220,16 @@ if (typeof jQuery === 'undefined') {
          *      - A callback function that returns the number
          *
          * - message: The invalid message
+         * - trim: Indicate the length will be calculated after trimming the value or not. It is false, by default
          * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false
          * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (options.trim === true || options.trim === 'true') {
+                value = $.trim(value);
+            }
+
             if (value === '') {
                 return true;
             }

File diff suppressed because it is too large
+ 3 - 3
dist/js/bootstrapValidator.min.js


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

@@ -11,6 +11,7 @@
             message: 'message',
             min: 'min',
             max: 'max',
+            trim: 'trim',
             utf8bytes: 'utf8Bytes'
         },
 
@@ -44,11 +45,16 @@
          *      - A callback function that returns the number
          *
          * - message: The invalid message
+         * - trim: Indicate the length will be calculated after trimming the value or not. It is false, by default
          * - utf8bytes: Evaluate string length in UTF-8 bytes, default to false
          * @returns {Object}
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (options.trim === true || options.trim === 'true') {
+                value = $.trim(value);
+            }
+
             if (value === '') {
                 return true;
             }

+ 38 - 0
test/spec.js

@@ -6567,6 +6567,44 @@ describe('stringLength', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
     });
+
+    it('trim option', function() {
+        this.bv.updateOption('textCharMaxLength', 'stringLength', 'trim', false);
+        this.$textCharMaxLength.val('');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('           ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('1234567890   ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.updateOption('textCharMaxLength', 'stringLength', 'trim', true);
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('   ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('                ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('  0123456789   ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('  01234567890  ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
 });
 
 describe('uri', function() {

+ 38 - 0
test/spec/validator/stringLength.js

@@ -179,4 +179,42 @@ describe('stringLength', function() {
         this.bv.validate();
         expect(this.bv.isValid()).toEqual(false);
     });
+
+    it('trim option', function() {
+        this.bv.updateOption('textCharMaxLength', 'stringLength', 'trim', false);
+        this.$textCharMaxLength.val('');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('           ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('1234567890   ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+
+        this.bv.updateOption('textCharMaxLength', 'stringLength', 'trim', true);
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('   ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('                ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('  0123456789   ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(true);
+
+        this.bv.resetForm();
+        this.$textCharMaxLength.val('  01234567890  ');
+        this.bv.validate();
+        expect(this.bv.isValid()).toEqual(false);
+    });
 });