phone.js 5.6 KB

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