uuid.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.uuid = $.extend($.fn.bootstrapValidator.i18n.uuid || {}, {
  3. 'default': 'Please enter a valid UUID number',
  4. version: 'Please enter a valid UUID version %s number'
  5. });
  6. $.fn.bootstrapValidator.validators.uuid = {
  7. html5Attributes: {
  8. message: 'message',
  9. version: 'version'
  10. },
  11. /**
  12. * Return true if and only if the input value is a valid UUID string
  13. *
  14. * @see http://en.wikipedia.org/wiki/Universally_unique_identifier
  15. * @param {BootstrapValidator} validator The validator plugin instance
  16. * @param {jQuery} $field Field element
  17. * @param {Object} options Consist of key:
  18. * - message: The invalid message
  19. * - version: Can be 3, 4, 5, null
  20. * @returns {Boolean|Object}
  21. */
  22. validate: function(validator, $field, options) {
  23. var value = $field.val();
  24. if (value === '') {
  25. return true;
  26. }
  27. // See the format at http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions
  28. var patterns = {
  29. '3': /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i,
  30. '4': /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  31. '5': /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i,
  32. all: /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
  33. },
  34. version = options.version ? (options.version + '') : 'all';
  35. return {
  36. valid: (null === patterns[version]) ? true : patterns[version].test(value),
  37. message: options.version
  38. ? $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.uuid.version, options.version)
  39. : (options.message || $.fn.bootstrapValidator.i18n.uuid['default'])
  40. };
  41. }
  42. };
  43. }(window.jQuery));