Mask for input DATA: "00/00/0000" === '0 to 31' / '0 to 12' / '19 || 20 '' 0 to 9 '' 0 to 9 '=

1

Hello, I need to make my <input id="data"> as parameters: taking into account the format of: 00/00/000; the first two from 0 to 31. (if they put value above, it resets to 31) The third: 0 or 1; The fourth number: 0 to 12;

and the fifth and sixth only: 19 or 20; the seventh and the eighth free numbers from 0 to 9;

               // ADICIONANDO METHOD DE DATA NO VALIDATION
    $.validator.addMethod("data",function(value, element) {
    value = jQuery.trim(value);
    var a = value;
    var b = a.charAt(0); // aqui eu ACREDITO ter selecionado o primeiro numero 
    return value.match(/^\d\d?\d\d\/\d\d\d\d$/);
    return this.optional(element) || retorno;
    },
    "Por favor, informe uma data válida"

    );

The bars are already inserted in the mask that I created:               jQuery (function ($) {

                 $("#data").mask("99/99/9999");
                 $("#fone").mask("(99) 9999-9999");
                 $("#cpf").mask("999.999.999-99");
                 $("#cep").mask("99.999-999");
                  });

JSFiddle this using jquery. :)

    
asked by anonymous 10.04.2015 / 07:10

1 answer

2

I've been almost one hour back from this plugin and I find it somewhat limited.

The plugin has a function that is called when the input is complete, the solution goes through. First I looked in the documentation and code about a evento with callback and nothing ... then I tried to change in the keyup event and it works, but the plugin saves a non public public value and resets the saved value even though I do input.value = 'novo valor'; I find this bad practice ...

But, apart from that, you can do it like this:

$("#data").mask("99/99/9999", {
    completed: function () {
        console.log('complete')
        var value = $(this).val().split('/');
        var maximos = [31, 12, 2100];
        var novoValor = value.map(function (parcela, i) {
            if (parseInt(parcela, 10) > maximos[i]) return maximos[i];
            return parcela;
        });
        if (novoValor.toString() != value.toString()) $(this).val(novoValor.join('/')).focus();
    }
});

jsFiddle: link

    
10.04.2015 / 09:15