between.js 2.0 KB

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