// 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();