Browse Source

Added a regex for validating UK phone numbers from the guide at
http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format)

Dave Rogers 11 years ago
parent
commit
a502af72b0

+ 20 - 5
demo/validators.html

@@ -74,9 +74,16 @@
                             </div>
 
                             <div class="form-group">
-                                <label class="col-lg-3 control-label">Phone number</label>
+                                <label class="col-lg-3 control-label">US Phone number</label>
                                 <div class="col-lg-5">
-                                    <input type="text" class="form-control" name="phoneNumber" />
+                                    <input type="text" class="form-control" name="phoneNumberUS" />
+                                </div>
+                            </div>
+
+                            <div class="form-group">
+                                <label class="col-lg-3 control-label">UK Phone number</label>
+                                <div class="col-lg-5">
+                                    <input type="text" class="form-control" name="phoneNumberUK" />
                                 </div>
                             </div>
 
@@ -194,13 +201,21 @@ $(document).ready(function() {
                     }
                 }
             },
-            phoneNumber: {
+            phoneNumberUS: {
                 validators: {
-                    digits: {
-                        message: 'The value can contain only digits'
+                    phone: {
+                        message: 'The input is not a valid US phone number'
                     }
                 }
             },
+            phoneNumberUK: {
+            	validators: {
+            		phone: {
+            			message: 'The input is not a valid UK phone number',
+            			country: 'GB'
+            		}
+            	}
+            },
             color: {
                 validators: {
                     hexColor: {

+ 9 - 2
dist/js/bootstrapValidator.js

@@ -3597,14 +3597,15 @@
         },
 
         /**
-         * Return true if the input value contains a valid US phone number only
+         * Return true if the input value contains a valid phone number for the country
+         * selected in the options
          *
          * @param {BootstrapValidator} validator Validate plugin instance
          * @param {jQuery} $field Field element
          * @param {Object} options Consist of key:
          * - message: The invalid message
          * - country: The ISO 3166 country code
-         * Currently it only supports United State (US) country
+         * Currently it only supports United State (US) or United Kingdom (GB) countries
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
@@ -3615,6 +3616,12 @@
 
             var country = (options.country || 'US').toUpperCase();
             switch (country) {
+            	case 'GB':
+            		// Make sure the input looks like a valid UK phone number based on the guide at
+            		// http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format
+            		// Test: http://regexr.com/38uhv
+            		value = value.trim();
+            		return (/^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$/).test(value);
                 case 'US':
                 default:
                     // Make sure US phone numbers have 10 digits

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


+ 9 - 2
src/js/validator/phone.js

@@ -6,14 +6,15 @@
         },
 
         /**
-         * Return true if the input value contains a valid US phone number only
+         * Return true if the input value contains a valid phone number for the country
+         * selected in the options
          *
          * @param {BootstrapValidator} validator Validate plugin instance
          * @param {jQuery} $field Field element
          * @param {Object} options Consist of key:
          * - message: The invalid message
          * - country: The ISO 3166 country code
-         * Currently it only supports United State (US) country
+         * Currently it only supports United State (US) or United Kingdom (GB) countries
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
@@ -24,6 +25,12 @@
 
             var country = (options.country || 'US').toUpperCase();
             switch (country) {
+            	case 'GB':
+            		// Make sure the input looks like a valid UK phone number based on the guide at
+            		// http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format
+            		// Test: http://regexr.com/38uhv
+            		value = value.trim();
+            		return (/^\(?(?:(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?\(?(?:0\)?[\s-]?\(?)?|0)(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}|\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4}|\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3})|\d{5}\)?[\s-]?\d{4,5}|8(?:00[\s-]?11[\s-]?11|45[\s-]?46[\s-]?4\d))(?:(?:[\s-]?(?:x|ext\.?\s?|\#)\d+)?)$/).test(value);
                 case 'US':
                 default:
                     // Make sure US phone numbers have 10 digits