stringCase.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. (function($) {
  2. $.fn.bootstrapValidator.validators.stringCase = {
  3. html5Attributes: {
  4. message: 'message',
  5. case: 'case'
  6. },
  7. /**
  8. * Check if a string is a lower or upper case one
  9. *
  10. * @param {BootstrapValidator} validator The validator plugin instance
  11. * @param {jQuery} $field Field element
  12. * @param {Object} options Consist of key:
  13. * - message: The invalid message
  14. * - case: Can be 'lower' (default) or 'upper'
  15. * @returns {Boolean}
  16. */
  17. validate: function(validator, $field, options) {
  18. var value = $field.val();
  19. if (value == '') {
  20. return true;
  21. }
  22. var stringCase = (options.case || 'lower').toLowerCase();
  23. switch (stringCase) {
  24. case 'upper':
  25. return value === value.toUpperCase();
  26. case 'lower':
  27. default:
  28. return value === value.toLowerCase();
  29. }
  30. }
  31. };
  32. }(window.jQuery));