javascript Aplicartáscaradefecha输入有效和cadadígito

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript Aplicartáscaradefecha输入有效和cadadígito相关的知识,希望对你有一定的参考价值。

// IMPORTANT! You need to use jquery mask plugin before execute this

//mask the input date using regex to allow only 01-31 day, 01-12 month, 1900-2000 year
    $('.date-mask').mask("DA/MO/YEXR", {

        // These are the initial regex
        translation: {
            D: {pattern: /[0-3]/},
            A: {pattern: /[0-9]/},
            M: {pattern: /[0-1]/},
            O: {pattern: /[0-9]/},
            Y: {pattern: /[1-2]/},
            E: {pattern: /[90]/},
            X: {pattern: /[0-9]/},
            R: {pattern: /[0-9]/}
        },

        // Here is when the magic happen, changing regex of some characters if needed
        // Documentation on -> https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html#callback-examples
        // To see original function taken as example and before modified -> https://gist.github.com/igorescobar/6450250#gistcomment-2307273
        onKeyPress: function(cep, event, currentField, options) {
            if (!cep) return;
            let m = cep.match(/(\d{1})/g);
            if (!m) return;

            if (parseInt(m[0]) === 3) {
                options.translation.A.pattern = /[0-1]/;
            } else if(parseInt(m[0]) === 0) {
                options.translation.A.pattern = /[1-9]/;
            } else{
                options.translation.A.pattern = /[0-9]/;
            }

            if (parseInt(m[2]) === 1) {
                options.translation.O.pattern = /[0-2]/;
            } else {
                options.translation.O.pattern = /[1-9]/;
            }

            if (parseInt(m[4]) === 2) {
                options.translation.E.pattern = /[0]/;
            } else {
                options.translation.E.pattern = /[9]/;
            }

            if (parseInt(m[5]) === 0) {
                options.translation.X.pattern = /[0]/;
            } else {
                options.translation.X.pattern = /[0-9]/;
            }

            if (parseInt(m[6]) === 0 && parseInt(m[5]) === 0) {
                options.translation.R.pattern = /[0]/;
            } else {
                options.translation.R.pattern = /[0-9]/;
            }

            let temp_value = currentField.val();
            currentField.val('');
            currentField.unmask().mask('DA/MO/YEXR', options);
            currentField.val(temp_value);
        }
    }).keyup();

以上是关于javascript Aplicartáscaradefecha输入有效和cadadígito的主要内容,如果未能解决你的问题,请参考以下文章

javascript Variosbásicos

javascript App con expressbásica

javascript MáscaradeTelefone e Celular

javascript Comprobar sin uncheckboxestáseleccionadocon jQuery

javascript desativgar提交enquanto输入estávazio

javascript Helpers paravalidaçãodeformulários