zipCode.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. (function($) {
  2. $.fn.bootstrapValidator.validators.zipCode = {
  3. html5Attributes: {
  4. message: 'message',
  5. country: 'country'
  6. },
  7. /**
  8. * Return true if and only if the input value is a valid country zip code
  9. *
  10. * @param {BootstrapValidator} validator The validator plugin instance
  11. * @param {jQuery} $field Field element
  12. * @param {Object} options Consist of key:
  13. * - message: The invalid message
  14. * - country: The ISO 3166 country code
  15. *
  16. * Currently it supports the following countries:
  17. * - US (United State)
  18. * - DK (Denmark)
  19. * - SE (Sweden)
  20. * @returns {Boolean}
  21. */
  22. validate: function(validator, $field, options) {
  23. var value = $field.val();
  24. if (value == '' || !options.country) {
  25. return true;
  26. }
  27. options.country = options.country || 'US';
  28. switch (options.country.toUpperCase()) {
  29. case 'DK':
  30. return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  31. case 'SE':
  32. return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  33. case 'US':
  34. default:
  35. return /^\d{4,5}([\-]\d{4})?$/.test(value);
  36. }
  37. }
  38. };
  39. }(window.jQuery));