different.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.different = $.extend($.fn.bootstrapValidator.i18n.different || {}, {
  3. 'default': 'Please enter a different value'
  4. });
  5. $.fn.bootstrapValidator.validators.different = {
  6. html5Attributes: {
  7. message: 'message',
  8. field: 'field'
  9. },
  10. /**
  11. * Return true if the input value is different with given field's value
  12. *
  13. * @param {BootstrapValidator} validator The validator plugin instance
  14. * @param {jQuery} $field Field element
  15. * @param {Object} options Consists of the following key:
  16. * - field: The name of field that will be used to compare with current one
  17. * - message: The invalid message
  18. * @returns {Boolean}
  19. */
  20. validate: function(validator, $field, options) {
  21. var value = $field.val();
  22. if (value === '') {
  23. return true;
  24. }
  25. var fields = options.field.split(','),
  26. isValid = true;
  27. for (var i = 0; i < fields.length; i++) {
  28. var compareWith = validator.getFieldElements(fields[i]);
  29. if (compareWith == null || compareWith.length === 0) {
  30. continue;
  31. }
  32. if (value === compareWith.val()) {
  33. isValid = false;
  34. } else {
  35. validator.updateStatus(fields[i], validator.STATUS_VALID, 'different');
  36. }
  37. }
  38. return isValid;
  39. }
  40. };
  41. }(window.jQuery));