phone.js 3.1 KB

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