| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- // https://stackoverflow.com/a/6871820
- // http://learn.jquery.com/plugins/basic-plugin-creation/
- (function($) {
- var validator_can_submit = true;
- var validator_warn = false;
- var validator_methods = {
- init: function(options) {
- $(this).on('submit', function(e) {
- validator_can_submit = true;
- validator_warn = false;
- $(this).find('input, select, textarea').each(function() {
- validator_clear($(this));
- validator_required($(this));
- validator_len($(this));
- validator_minlen($(this));
- validator_maxlen($(this));
- validator_func($(this));
- });
- if (!validator_can_submit) {
- return false;
- }
- if (validator_warn) {
- if (!confirm("Inválido")) {
- return false
- }
- }
- });
- }
- };
- $.fn.validator = function(methodOrOptions) {
- if (validator_methods[methodOrOptions]) {
- return validator_methods[methodOrOptions].apply(this, Array.prototype.slice.call(arguments, 1));
- } else if (typeof methodOrOptions === 'object' || !methodOrOptions) {
- // Default to "init"
- return validator_methods.init.apply(this, arguments);
- } else {
- console.log("--- Plugin error");
- }
- };
- function validator_clear(element) {
- $(element).parent().removeClass('has-success');
- $(element).parent().removeClass('has-error');
- $(element).parent().removeClass('has-warning');
- }
- function validator_required(element) {
- if (typeof $(element).data('required') === 'undefined') {
- return;
- }
- if ($(element).data('required') === true) {
- if ($(element).val() === "") {
- $(element).parent().addClass('has-error');
- validator_can_submit = false;
- }
- return;
- }
- if ($(element).val() === '') {
- $(element).parent().addClass('has-warning');
- validator_warn = true;
- }
- }
- function validator_minlen(element) {
- if (typeof $(element).data('minlen') === 'undefined') {
- return;
- }
- if ($(element).val().length === 0) {
- return;
- }
- if ($(element).val().length < $(element).data('minlen')) {
- validator_can_submit = false;
- $(element).parent().addClass('has-error');
- }
- }
- function validator_len(element) {
- if (typeof $(element).data('len') === 'undefined') {
- return;
- }
- if ($(element).val().length === 0) {
- return;
- }
- if ($(element).val().length !== $(element).data('len')) {
- validator_can_submit = false;
- $(element).parent().addClass('has-error');
- }
- }
- function validator_maxlen(element) {
- if (typeof $(element).data('maxlen') === 'undefined') {
- return;
- }
- if ($(element).val().length === 0) {
- return;
- }
- if ($(element).val().length > $(element).data('maxlen')) {
- validator_can_submit = false;
- $(element).parent().addClass('has-error');
- }
- }
- function validator_func(element) {
- if (typeof $(element).data('func') === 'undefined') {
- return;
- }
- if ($(element).val().length === 0) {
- return;
- }
- if (eval("typeof validate_" + $(element).data('func')) === "function") {
- /*
- if (! eval("validate_" + $(element).data('func') + "( "+$(element).val()+" )")) {
- validator_can_submit = false;
- $(element).parent().addClass('has-error');
- }*/
- }
- }
- //---------------------
- function validate_cpf(value = "") {
- value = value.replace(/\./g, '').replace(/-/g, '');
- var numeros, digitos, soma, i, resultado, digitos_iguais;
- digitos_iguais = 1;
- if (value.length < 11) {
- return false;
- }
- for (i = 0; i < value.length - 1; i++) {
- if (value.charAt(i) != value.charAt(i + 1)) {
- digitos_iguais = 0;
- break;
- }
- }
- if (!digitos_iguais) {
- numeros = value.substring(0, 9);
- digitos = value.substring(9);
- soma = 0;
- for (i = 10; i > 1; i--) {
- soma += numeros.charAt(10 - i) * i;
- }
- resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
- if (resultado != digitos.charAt(0)) {
- return false;
- }
- numeros = value.substring(0, 10);
- soma = 0;
- for (i = 11; i > 1; i--) {
- soma += numeros.charAt(11 - i) * i;
- }
- resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
- if (resultado != digitos.charAt(1)) {
- return false;
- }
- return true;
- } else {
- return false;
- }
- }
- })(jQuery);
|