stringLength.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. (function($) {
  2. $.fn.bootstrapValidator.validators.stringLength = {
  3. html5Attributes: {
  4. message: 'message',
  5. min: 'min',
  6. max: 'max'
  7. },
  8. enableByHtml5: function($field) {
  9. var maxLength = $field.attr('maxlength');
  10. if (maxLength) {
  11. return {
  12. max: parseInt(maxLength, 10)
  13. };
  14. }
  15. return false;
  16. },
  17. /**
  18. * Check if the length of element value is less or more than given number
  19. *
  20. * @param {BootstrapValidator} validator The validator plugin instance
  21. * @param {jQuery} $field Field element
  22. * @param {Object} options Consists of following keys:
  23. * - min
  24. * - max
  25. * At least one of two keys is required
  26. * - message: The invalid message
  27. * @returns {Boolean}
  28. */
  29. validate: function(validator, $field, options) {
  30. var value = $field.val();
  31. if (value == '') {
  32. return true;
  33. }
  34. var length = $.trim(value).length;
  35. if ((options.min && length < options.min) || (options.max && length > options.max)) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. };
  41. }(window.jQuery));