ソースを参照

#139: Add stringCase validator

nghuuphuoc 11 年 前
コミット
a1ac0a17fd

+ 19 - 0
demo/selector.html

@@ -21,6 +21,13 @@
 
                     <form id="paymentForm" method="post" class="form-horizontal" action="target.php">
                         <div class="form-group">
+                            <label class="col-lg-3 control-label">Card holder</label>
+                            <div class="col-lg-5">
+                                <input type="text" class="form-control" id="cardHolder" />
+                            </div>
+                        </div>
+
+                        <div class="form-group">
                             <label class="col-lg-3 control-label">Credit card number</label>
                             <div class="col-lg-5">
                                 <input type="text" class="form-control" id="ccNumber" />
@@ -64,6 +71,18 @@ $(document).ready(function() {
             validating: 'glyphicon glyphicon-refresh'
         },
         fields: {
+            cardHolder: {
+                selector: '#cardHolder',
+                validators: {
+                    notEmpty: {
+                        message: 'The card holder is required'
+                    },
+                    stringCase: {
+                        message: 'The card holder must contain upper case characters only',
+                        case: 'upper'
+                    }
+                }
+            },
             ccNumber: {
                 selector: '#ccNumber',
                 validators: {

+ 34 - 0
dist/js/bootstrapValidator.js

@@ -1762,6 +1762,40 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.stringCase = {
+        html5Attributes: {
+            message: 'message',
+            case: 'case'
+        },
+
+        /**
+         * Check if a string is a lower or upper case one
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * - case: Can be 'lower' (default) or 'upper'
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var stringCase = (options.case || 'lower').toLowerCase();
+            switch (stringCase) {
+                case 'upper':
+                    return value === value.toUpperCase();
+                case 'lower':
+                default:
+                    return value === value.toLowerCase();
+            }
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.stringLength = {
         html5Attributes: {
             message: 'message',

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


+ 34 - 0
src/js/validator/stringCase.js

@@ -0,0 +1,34 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.stringCase = {
+        html5Attributes: {
+            message: 'message',
+            case: 'case'
+        },
+
+        /**
+         * Check if a string is a lower or upper case one
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * - case: Can be 'lower' (default) or 'upper'
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var stringCase = (options.case || 'lower').toLowerCase();
+            switch (stringCase) {
+                case 'upper':
+                    return value === value.toUpperCase();
+                case 'lower':
+                default:
+                    return value === value.toLowerCase();
+            }
+        }
+    };
+}(window.jQuery));