validator.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // https://stackoverflow.com/a/6871820
  2. // http://learn.jquery.com/plugins/basic-plugin-creation/
  3. (function($) {
  4. var validator_can_submit = true;
  5. var validator_warn = false;
  6. var validator_methods = {
  7. init: function(options) {
  8. $(this).on('submit', function(e) {
  9. validator_can_submit = true;
  10. validator_warn = false;
  11. $(this).find('input, select, textarea').each(function() {
  12. validator_clear($(this));
  13. validator_required($(this));
  14. validator_len($(this));
  15. validator_minlen($(this));
  16. validator_maxlen($(this));
  17. validator_func($(this));
  18. });
  19. if (!validator_can_submit) {
  20. return false;
  21. }
  22. if (validator_warn) {
  23. if (!confirm("Inválido")) {
  24. return false
  25. }
  26. }
  27. });
  28. }
  29. };
  30. $.fn.validator = function(methodOrOptions) {
  31. if (validator_methods[methodOrOptions]) {
  32. return validator_methods[methodOrOptions].apply(this, Array.prototype.slice.call(arguments, 1));
  33. } else if (typeof methodOrOptions === 'object' || !methodOrOptions) {
  34. // Default to "init"
  35. return validator_methods.init.apply(this, arguments);
  36. } else {
  37. console.log("--- Plugin error");
  38. }
  39. };
  40. function validator_clear(element) {
  41. $(element).parent().removeClass('has-success');
  42. $(element).parent().removeClass('has-error');
  43. $(element).parent().removeClass('has-warning');
  44. }
  45. function validator_required(element) {
  46. if (typeof $(element).data('required') === 'undefined') {
  47. return;
  48. }
  49. if ($(element).data('required') === true) {
  50. if ($(element).val() === "") {
  51. $(element).parent().addClass('has-error');
  52. validator_can_submit = false;
  53. }
  54. return;
  55. }
  56. if ($(element).val() === '') {
  57. $(element).parent().addClass('has-warning');
  58. validator_warn = true;
  59. }
  60. }
  61. function validator_minlen(element) {
  62. if (typeof $(element).data('minlen') === 'undefined') {
  63. return;
  64. }
  65. if ($(element).val().length === 0) {
  66. return;
  67. }
  68. if ($(element).val().length < $(element).data('minlen')) {
  69. validator_can_submit = false;
  70. $(element).parent().addClass('has-error');
  71. }
  72. }
  73. function validator_len(element) {
  74. if (typeof $(element).data('len') === 'undefined') {
  75. return;
  76. }
  77. if ($(element).val().length === 0) {
  78. return;
  79. }
  80. if ($(element).val().length !== $(element).data('len')) {
  81. validator_can_submit = false;
  82. $(element).parent().addClass('has-error');
  83. }
  84. }
  85. function validator_maxlen(element) {
  86. if (typeof $(element).data('maxlen') === 'undefined') {
  87. return;
  88. }
  89. if ($(element).val().length === 0) {
  90. return;
  91. }
  92. if ($(element).val().length > $(element).data('maxlen')) {
  93. validator_can_submit = false;
  94. $(element).parent().addClass('has-error');
  95. }
  96. }
  97. function validator_func(element) {
  98. if (typeof $(element).data('func') === 'undefined') {
  99. return;
  100. }
  101. if ($(element).val().length === 0) {
  102. return;
  103. }
  104. if (eval("typeof validate_" + $(element).data('func')) === "function") {
  105. /*
  106. if (! eval("validate_" + $(element).data('func') + "( "+$(element).val()+" )")) {
  107. validator_can_submit = false;
  108. $(element).parent().addClass('has-error');
  109. }*/
  110. }
  111. }
  112. //---------------------
  113. function validate_cpf(value = "") {
  114. value = value.replace(/\./g, '').replace(/-/g, '');
  115. var numeros, digitos, soma, i, resultado, digitos_iguais;
  116. digitos_iguais = 1;
  117. if (value.length < 11) {
  118. return false;
  119. }
  120. for (i = 0; i < value.length - 1; i++) {
  121. if (value.charAt(i) != value.charAt(i + 1)) {
  122. digitos_iguais = 0;
  123. break;
  124. }
  125. }
  126. if (!digitos_iguais) {
  127. numeros = value.substring(0, 9);
  128. digitos = value.substring(9);
  129. soma = 0;
  130. for (i = 10; i > 1; i--) {
  131. soma += numeros.charAt(10 - i) * i;
  132. }
  133. resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  134. if (resultado != digitos.charAt(0)) {
  135. return false;
  136. }
  137. numeros = value.substring(0, 10);
  138. soma = 0;
  139. for (i = 11; i > 1; i--) {
  140. soma += numeros.charAt(11 - i) * i;
  141. }
  142. resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
  143. if (resultado != digitos.charAt(1)) {
  144. return false;
  145. }
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. }
  151. })(jQuery);