浏览代码

Merge pull request #532 from troymccabe/enhancement-imo

Adding IMO validator
Phuoc Nguyen 11 年之前
父节点
当前提交
dfb47b3592
共有 3 个文件被更改,包括 147 次插入0 次删除
  1. 48 0
      src/js/validator/imo.js
  2. 50 0
      test/spec.js
  3. 49 0
      test/spec/validator/imo.js

+ 48 - 0
src/js/validator/imo.js

@@ -0,0 +1,48 @@
+(function($) {
+    $.fn.bootstrapValidator.i18n.imo = $.extend($.fn.bootstrapValidator.i18n.imo || {}, {
+        'default': 'Please enter a valid IMO number'
+    });
+
+    $.fn.bootstrapValidator.validators.imo = {
+        /**
+         * Validate IMO (International Maritime Organization)
+         * Examples:
+         * - Valid: IMO 8814275, IMO 9176187
+         * - Invalid: IMO 8814274
+         *
+         * @see http://en.wikipedia.org/wiki/IMO_Number
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Can consist of the following keys:
+         * - message: The invalid message
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value === '') {
+                return true;
+            }
+            
+            // if we fail the regexp, fail out
+            if (!/^IMO \d{7}$/i.test(value)) {
+                return false;
+            }
+            
+            // grab just the digits
+            var sum = 0;
+            var digits = value.replace(/^.*(\d{7})$/, '$1');
+            
+            // go over each char, multiplying by the inverse of it's position
+            // IMO 9176187
+            // (9 * 7) + (1 * 6) + (7 * 5) + (6 * 4) + (1 * 3) + (8 * 2) = 147
+            // take the last digit of that, that's the check digit (7)
+            for (var i = 6; i >= 1; i--) {
+                sum += (digits.slice((6 - i), -i) * (i + 1));
+            }
+            var checkDigit = sum % 10;
+            
+            // verify the checkdigit
+            return parseInt(digits.charAt(6)) === checkDigit;
+        }
+    };
+}(window.jQuery));

+ 50 - 0
test/spec.js

@@ -3209,6 +3209,56 @@ describe('id', function() {
     });
 });
 
+describe('imo', function() {
+    beforeEach(function () {
+        $([
+            '<form class="form-horizontal" id="imoForm">',
+                '<div id="msg"></div>',
+                '<div class="form-group">',
+                    '<input type="text" name="imo" data-bv-imo />',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#imoForm').bootstrapValidator();
+
+        this.bv   = $('#imoForm').data('bootstrapValidator');
+        this.$imo = this.bv.getFieldElements('imo');
+    });
+
+    afterEach(function () {
+        $('#imoForm').bootstrapValidator('destroy').remove();
+    });
+
+    it('Valid IMO (upper)', function() {
+        this.bv.resetForm();
+        this.$imo.val('IMO 9074729');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Valid IMO (lower)', function() {
+        this.bv.resetForm();
+        this.$imo.val('imo 9074729');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Invalid IMO (bad format)', function() {
+        this.bv.resetForm();
+        this.$imo.val('9074729');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeFalsy();
+    });
+
+    it('Invalid IMO (bad check digit)', function() {
+        this.bv.resetForm();
+        this.$imo.val('IMO 9074728');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeFalsy();
+    });
+});
+
 describe('isbn', function() {
     beforeEach(function() {
         var html = [

+ 49 - 0
test/spec/validator/imo.js

@@ -0,0 +1,49 @@
+describe('imo', function() {
+    beforeEach(function () {
+        $([
+            '<form class="form-horizontal" id="imoForm">',
+                '<div id="msg"></div>',
+                '<div class="form-group">',
+                    '<input type="text" name="imo" data-bv-imo />',
+                '</div>',
+            '</form>'
+        ].join('\n')).appendTo('body');
+
+        $('#imoForm').bootstrapValidator();
+
+        this.bv   = $('#imoForm').data('bootstrapValidator');
+        this.$imo = this.bv.getFieldElements('imo');
+    });
+
+    afterEach(function () {
+        $('#imoForm').bootstrapValidator('destroy').remove();
+    });
+
+    it('Valid IMO (upper)', function() {
+        this.bv.resetForm();
+        this.$imo.val('IMO 9074729');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Valid IMO (lower)', function() {
+        this.bv.resetForm();
+        this.$imo.val('imo 9074729');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeTruthy();
+    });
+
+    it('Invalid IMO (bad format)', function() {
+        this.bv.resetForm();
+        this.$imo.val('9074729');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeFalsy();
+    });
+
+    it('Invalid IMO (bad check digit)', function() {
+        this.bv.resetForm();
+        this.$imo.val('IMO 9074728');
+        this.bv.validate();
+        expect(this.bv.isValid()).toBeFalsy();
+    });
+});