Browse Source

#99, #100: Add country option to the phone validator

nghuuphuoc 12 years ago
parent
commit
e087d4ac51

+ 32 - 1
dist/js/bootstrapValidator.js

@@ -984,6 +984,35 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.phone = {
+        /**
+         * Return true if the input value contains a valid US phone number only
+         *
+         * @param {BootstrapValidator} validator Validate plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - country: The ISO 3166 country code
+         *
+         * Currently it only supports United State (US) country
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            options.country = options.country || 'US';
+            switch (options.country.toUpperCase()) {
+                case 'US':
+                default:
+                    value = value.replace(/\(|\)|\s+/g, '');
+                    return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
+            }
+        }
+    }
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.regexp = {
         /**
          * Check if the element value matches given regular expression
@@ -1193,11 +1222,13 @@
          *
          * @returns {Boolean}
          */
-        validate: function(validateInstance, $field, options) {
+        validate: function(validator, $field, options) {
             var value = $field.val();
             if (value == '' || !options.country) {
                 return true;
             }
+
+            options.country = options.country || 'US';
             switch (options.country.toUpperCase()) {
                 case 'DK':
                     return /^(DK(-|\s)?)?\d{4}$/i.test(value);

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


+ 11 - 3
src/js/validator/phone.js

@@ -5,7 +5,10 @@
          *
          * @param {BootstrapValidator} validator Validate plugin instance
          * @param {jQuery} $field Field element
-         * @param {Object} options
+         * @param {Object} options Consist of key:
+         * - country: The ISO 3166 country code
+         *
+         * Currently it only supports United State (US) country
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
@@ -14,8 +17,13 @@
                 return true;
             }
 
-            value = value.replace(/\(|\)|\s+/g, '');
-            return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
+            options.country = options.country || 'US';
+            switch (options.country.toUpperCase()) {
+                case 'US':
+                default:
+                    value = value.replace(/\(|\)|\s+/g, '');
+                    return (/^(?:1\-?)?(\d{3})[\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value);
+            }
         }
     }
 }(window.jQuery));

+ 3 - 1
src/js/validator/zipCode.js

@@ -15,11 +15,13 @@
          *
          * @returns {Boolean}
          */
-        validate: function(validateInstance, $field, options) {
+        validate: function(validator, $field, options) {
             var value = $field.val();
             if (value == '' || !options.country) {
                 return true;
             }
+
+            options.country = options.country || 'US';
             switch (options.country.toUpperCase()) {
                 case 'DK':
                     return /^(DK(-|\s)?)?\d{4}$/i.test(value);