regexp.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.regexp = $.extend($.fn.bootstrapValidator.i18n.regexp || {}, {
  3. 'default': 'The value does not match the pattern'
  4. });
  5. $.fn.bootstrapValidator.validators.regexp = {
  6. html5Attributes: {
  7. message: 'message',
  8. regexp: 'regexp'
  9. },
  10. enableByHtml5: function($field) {
  11. var pattern = $field.attr('pattern');
  12. if (pattern) {
  13. return {
  14. regexp: pattern
  15. };
  16. }
  17. return false;
  18. },
  19. /**
  20. * Check if the element value matches given regular expression
  21. *
  22. * @param {BootstrapValidator} validator The validator plugin instance
  23. * @param {jQuery} $field Field element
  24. * @param {Object} options Consists of the following key:
  25. * - regexp: The regular expression you need to check
  26. * @returns {Boolean}
  27. */
  28. validate: function(validator, $field, options) {
  29. var value = $field.val();
  30. if (value == '') {
  31. return true;
  32. }
  33. var regexp = ('string' == typeof options.regexp) ? new RegExp(options.regexp) : options.regexp;
  34. return regexp.test(value);
  35. }
  36. };
  37. }(window.jQuery));