| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- (function($) {
- $.fn.bootstrapValidator.i18n.phone = $.extend($.fn.bootstrapValidator.i18n.phone || {}, {
- 'default': 'The value is not a valid phone number',
- countryNotSupported: 'The country code %s is not supported',
- country: 'The value is not a valid phone number in %s',
- countries: {
- GB: 'United Kingdom',
- US: 'USA'
- },
- getMessage: function(options) {
- var country = (options.country || 'US').toUpperCase();
- if ($.inArray(country, $.fn.bootstrapValidator.validators.phone.COUNTRIES) == -1) {
- return $.fn.bootstrapValidator.helpers.format(this.countryNotSupported, country);
- }
- if (this.countries[country]) {
- return $.fn.bootstrapValidator.helpers.format(this.country, this.countries[country]);
- }
- return this['default'];
- }
- });
- $.fn.bootstrapValidator.validators.phone = {
- html5Attributes: {
- message: 'message',
- country: 'country'
- },
- // The supported countries
- COUNTRIES: ['GB', 'US'],
- /**
- * 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) or United Kingdom (GB) countries
- * @returns {Boolean}
- */
- validate: function(validator, $field, options) {
- var value = $field.val();
- if (value == '') {
- return true;
- }
- var country = (options.country || 'US').toUpperCase();
- if ($.inArray(country, this.COUNTRIES) == -1) {
- return false;
- }
- switch (country) {
- case 'GB':
- // 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 = $.trim(value);
- 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
- // May start with 1, +1, or 1-; should discard
- // Area code may be delimited with (), & sections may be delimited with . or -
- // Test: http://regexr.com/38mqi
- value = value.replace(/\D/g, '');
- return (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length == 10);
- }
- }
- }
- }(window.jQuery));
|