greaterThan.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 type = $field.attr('type'),
  14. min = $field.attr('min');
  15. if (min && type !== 'date') {
  16. return {
  17. value: min
  18. };
  19. }
  20. return false;
  21. },
  22. /**
  23. * Return true if the input value is greater than or equals to given number
  24. *
  25. * @param {BootstrapValidator} validator Validate plugin instance
  26. * @param {jQuery} $field Field element
  27. * @param {Object} options Can consist of the following keys:
  28. * - value: Define the number to compare with. It can be
  29. * - A number
  30. * - Name of field which its value defines the number
  31. * - Name of callback function that returns the number
  32. * - A callback function that returns the number
  33. *
  34. * - inclusive [optional]: Can be true or false. Default is true
  35. * - message: The invalid message
  36. * @returns {Boolean|Object}
  37. */
  38. validate: function(validator, $field, options) {
  39. var value = $field.val();
  40. if (value === '') {
  41. return true;
  42. }
  43. if (!$.isNumeric(value)) {
  44. return false;
  45. }
  46. var compareTo = $.isNumeric(options.value) ? options.value : validator.getDynamicOption($field, options.value);
  47. value = parseFloat(value);
  48. return (options.inclusive === true || options.inclusive === undefined)
  49. ? {
  50. valid: value >= compareTo,
  51. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.greaterThan['default'], compareTo)
  52. }
  53. : {
  54. valid: value > compareTo,
  55. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.greaterThan.notInclusive, compareTo)
  56. };
  57. }
  58. };
  59. }(window.jQuery));