phone.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.phone = $.extend($.fn.bootstrapValidator.i18n.phone || {}, {
  3. 'default': 'Please enter a valid phone number',
  4. countryNotSupported: 'The country code %s is not supported',
  5. country: 'Please enter a valid phone number in %s',
  6. countries: {
  7. GB: 'United Kingdom',
  8. FR: 'France',
  9. US: 'USA'
  10. }
  11. });
  12. $.fn.bootstrapValidator.validators.phone = {
  13. html5Attributes: {
  14. message: 'message',
  15. country: 'country'
  16. },
  17. // The supported countries
  18. COUNTRY_CODES: ['GB', 'FR', US'],
  19. /**
  20. * Return true if the input value contains a valid phone number for the country
  21. * selected in the options
  22. *
  23. * @param {BootstrapValidator} validator Validate plugin instance
  24. * @param {jQuery} $field Field element
  25. * @param {Object} options Consist of key:
  26. * - message: The invalid message
  27. * - country: The ISO-3166 country code. It can be
  28. * - A country code
  29. * - Name of field which its value defines the country code
  30. * - Name of callback function that returns the country code
  31. * - A callback function that returns the country code
  32. *
  33. * Currently it only supports United State (US) or United Kingdom (GB) countries
  34. * @returns {Boolean|Object}
  35. */
  36. validate: function(validator, $field, options) {
  37. var value = $field.val();
  38. if (value === '') {
  39. return true;
  40. }
  41. var country = options.country;
  42. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  43. // Try to determine the country
  44. country = validator.getDynamicOption($field, country);
  45. }
  46. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  47. return {
  48. valid: false,
  49. message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.phone.countryNotSupported, country)
  50. };
  51. }
  52. var isValid = true;
  53. switch (country.toUpperCase()) {
  54. case 'GB':
  55. // http://aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_GB_Telephone_Numbers#Match_GB_telephone_number_in_any_format
  56. // Test: http://regexr.com/38uhv
  57. value = $.trim(value);
  58. isValid = (/^\(?(?:(?: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);
  59. break;
  60. case 'FR':
  61. // http://regexr.com/39a2p
  62. value = $.trim(value);
  63. isValid = (/^(?:(?:(?:\+|00)33[ ]?(?:\(0\)[ ]?)?)|0){1}[1-9]{1}([ .-]?)(?:\d{2}\1?){3}\d{2}$/).test(value);
  64. break;
  65. case 'US':
  66. /* falls through */
  67. default:
  68. // Make sure US phone numbers have 10 digits
  69. // May start with 1, +1, or 1-; should discard
  70. // Area code may be delimited with (), & sections may be delimited with . or -
  71. // Test: http://regexr.com/38mqi
  72. value = value.replace(/\D/g, '');
  73. isValid = (/^(?:(1\-?)|(\+1 ?))?\(?(\d{3})[\)\-\.]?(\d{3})[\-\.]?(\d{4})$/).test(value) && (value.length === 10);
  74. break;
  75. }
  76. return {
  77. valid: isValid,
  78. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.phone.country, $.fn.bootstrapValidator.i18n.phone.countries[country])
  79. };
  80. }
  81. };
  82. }(window.jQuery));