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

Asked

Viewed 4,052 times

1

Hi, I need to make mine <input id="data"> has as parameters: taking into account the format of : 00/00/000; the first two from 0 to 31. (if you 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 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 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. :)

  • and you want the bars to be inserted by the user or the mask?

  • The bars are already inserted by the mask, I only need the control of the value that is inserted

  • http://jsfiddle.net/9juy4m5j/

  • the main purpose is to create a mask or validate the date or both? If you want to validate, you can try this plugin: http://jqueryvalidation.org/date-method/

  • the purpose and make the user not able to put a value above 31 in the first 2 characters and so on, is explained above.

1 answer

2


I’ve been almost an hour back from this plugin and found it kind of limited.

The plugin has a function that is called when the input is complete, the solution goes through there. I first searched the documentation and code about a evento with callback and nothing... then tried to change in the event keyup and it works, but the plugin keeps an internal value not published and restores the saved value despite I do input.value = 'novo valor'; I find that bad practice...

But anyway, you criticize the part you can do 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: http://jsfiddle.net/10ew5u0x/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.