lessThan.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.lessThan = $.extend($.fn.bootstrapValidator.i18n.lessThan || {}, {
  3. 'default': 'The value must be less than or equal to %s',
  4. notInclusive: 'The value must be less than %s',
  5. getMessage: function(options) {
  6. return (options.inclusive === true || options.inclusive == undefined)
  7. ? $.fn.bootstrapValidator.helpers.format(this['default'], options.value)
  8. : $.fn.bootstrapValidator.helpers.format(this.notInclusive, options.value);
  9. }
  10. });
  11. $.fn.bootstrapValidator.validators.lessThan = {
  12. html5Attributes: {
  13. message: 'message',
  14. value: 'value',
  15. inclusive: 'inclusive'
  16. },
  17. enableByHtml5: function($field) {
  18. var max = $field.attr('max');
  19. if (max) {
  20. return {
  21. value: max
  22. };
  23. }
  24. return false;
  25. },
  26. /**
  27. * Return true if the input value is less than or equal to given number
  28. *
  29. * @param {BootstrapValidator} validator The validator plugin instance
  30. * @param {jQuery} $field Field element
  31. * @param {Object} options Can consist of the following keys:
  32. * - value: The number used to compare to
  33. * - inclusive [optional]: Can be true or false. Default is true
  34. * - message: The invalid message
  35. * @returns {Boolean}
  36. */
  37. validate: function(validator, $field, options) {
  38. var value = $field.val();
  39. if (value == '') {
  40. return true;
  41. }
  42. value = parseFloat(value);
  43. return (options.inclusive === true || options.inclusive == undefined) ? (value <= options.value) : (value < options.value);
  44. }
  45. };
  46. }(window.jQuery));