phone.js 4.5 KB

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