greaterThan.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.greaterThan = $.extend($.fn.bootstrapValidator.i18n.greaterThan || {}, {
  3. 'default': 'Please enter a value greater than or equal to %s',
  4. notInclusive: 'Please enter a value greater than %s'
  5. });
  6. $.fn.bootstrapValidator.validators.greaterThan = {
  7. html5Attributes: {
  8. message: 'message',
  9. value: 'value',
  10. inclusive: 'inclusive'
  11. },
  12. enableByHtml5: function($field) {
  13. var min = $field.attr('min');
  14. if (min) {
  15. return {
  16. value: min
  17. };
  18. }
  19. return false;
  20. },
  21. /**
  22. * Return true if the input value is greater than or equals to given number
  23. *
  24. * @param {BootstrapValidator} validator Validate plugin instance
  25. * @param {jQuery} $field Field element
  26. * @param {Object} options Can consist of the following keys:
  27. * - value: Define the number to compare with. It can be
  28. * - A number
  29. * - Name of field which its value defines the number
  30. * - Name of callback function that returns the number
  31. * - A callback function that returns the number
  32. *
  33. * - inclusive [optional]: Can be true or false. Default is true
  34. * - message: The invalid message
  35. * @returns {Boolean|Object}
  36. */
  37. validate: function(validator, $field, options) {
  38. var value = $field.val();
  39. if (value === '') {
  40. return true;
  41. }
  42. var compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption(options.value, $field);
  43. value = parseFloat(value);
  44. return (options.inclusive === true || options.inclusive === undefined)
  45. ? {
  46. valid: value >= compareTo,
  47. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.greaterThan['default'], compareTo)
  48. }
  49. : {
  50. valid: value > compareTo,
  51. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.greaterThan.notInclusive, compareTo)
  52. };
  53. }
  54. };
  55. }(window.jQuery));