RobinHerbots/jquery.inputmask — pull request #427 by NunoTrovisco (2014-02-03 11:54:15+00:00)
Title: year-day-month date support

Hi there. I've been using your plugin and noticed the following thing missing.
In jquery.inputmask.date.extensions, there is no native support for formats of type year-day-month (the three separators were considered). Perhaps it didn't need so much code but, i tried it and worked for me. Accept it if you think it's usefull, otherwise no harm feelings

'yyyy/dd/mm': {
mask: "y/1/2",
placeholder: "yyyy/dd/mm",
regex: {
val1pre: new RegExp("[0-3]"), //daypre
val1: new RegExp("0[1-9]|[12][0-9]|3[01]"), //day
val2pre: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|[12][0-9]|3[01])" + escapedSeparator + "[01])"); }, //monthpre
val2: function (separator) { var escapedSeparator = $.inputmask.escapeRegex.call(this, separator); return new RegExp("((0[1-9]|[12][0-9])" + escapedSeparator + "(0[1-9]|1[012]))|(30" + escapedSeparator + "(0[13-9]|1[012]))|(31" + escapedSeparator + "(0[13578]|1[02]))"); }//month
},
leapday: "/29/02",
separator: '/',
yearrange: { minyear: 1900, maxyear: 2099 },
isInYearRange: function (chrs, minyear, maxyear) {
var enteredyear = parseInt(chrs.concat(minyear.toString().slice(chrs.length)));
var enteredyear2 = parseInt(chrs.concat(maxyear.toString().slice(chrs.length)));
return (enteredyear != NaN ? minyear <= enteredyear && enteredyear <= maxyear : false) ||
(enteredyear2 != NaN ? minyear <= enteredyear2 && enteredyear2 <= maxyear : false);
},
determinebaseyear: function (minyear, maxyear, hint) {
var currentyear = (new Date()).getFullYear();
if (minyear > currentyear) return minyear;
if (maxyear < currentyear) {
var maxYearPrefix = maxyear.toString().slice(0, 2);
var maxYearPostfix = maxyear.toString().slice(2, 4);
while (maxyear < maxYearPrefix + hint) {
maxYearPrefix--;
}
var maxxYear = maxYearPrefix + maxYearPostfix;
return minyear > maxxYear ? minyear : maxxYear;
}

return currentyear;
},
onKeyUp: function (e, buffer, opts) {
var $input = $(this);
if (e.ctrlKey && e.keyCode == opts.keyCode.RIGHT) {
var today = new Date();
$input.val(today.getFullYear().toString() + today.getDate().toString() + (today.getMonth() + 1).toString() );
}
},
definitions: {
'1': { //val1 ~ day or month
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.regex.val1.test(chrs);
if (!strict && !isValid) {
if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
isValid = opts.regex.val1.test("0" + chrs.charAt(0));
if (isValid) {
buffer[pos - 1] = "0";
return { "pos": pos, "c": chrs.charAt(0) };
}
}
}
return isValid;
},
cardinality: 2,
prevalidator: [{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.regex.val1pre.test(chrs);
if (!strict && !isValid) {
isValid = opts.regex.val1.test("0" + chrs);
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
return isValid;
}, cardinality: 1
}]
},
'2': { //val2 ~ day or month
validator: function (chrs, buffer, pos, strict, opts) {
var frontValue = buffer.join('').substr(5, 3);
var isValid = opts.regex.val2(opts.separator).test(frontValue + chrs);
if (!strict && !isValid) {
if (chrs.charAt(1) == opts.separator || "-./".indexOf(chrs.charAt(1)) != -1) {
isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs.charAt(0));
if (isValid) {
buffer[pos - 1] = "0";
return { "pos": pos, "c": chrs.charAt(0) };
}
}
}
return isValid;
},
cardinality: 2,
prevalidator: [{
validator: function (chrs, buffer, pos, strict, opts) {
var frontValue = buffer.join('').substr(5, 3);
var isValid = opts.regex.val2pre(opts.separator).test(frontValue + chrs);
if (!strict && !isValid) {
isValid = opts.regex.val2(opts.separator).test(frontValue + "0" + chrs);
if (isValid) {
buffer[pos] = "0";
pos++;
return { "pos": pos };
}
}
return isValid;
}, cardinality: 1
}]
},
'y': { //year
validator: function (chrs, buffer, pos, strict, opts) {
if (opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
var dayMonthValue = buffer.join('').substr(0, 4);
if (dayMonthValue != opts.leapday)
return true;
else {
var year = parseInt(chrs, 10);//detect leap year
if (year % 4 === 0)
if (year % 100 === 0)
if (year % 400 === 0)
return true;
else return false;
else return true;
else return false;
}
} else return false;
},
cardinality: 4,
prevalidator: [
{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (!strict && !isValid) {
var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs + "0").toString().slice(0, 1);

isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (isValid) {
buffer[pos++] = yearPrefix[0];
return { "pos": pos };
}
yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs + "0").toString().slice(0, 2);

isValid = opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (isValid) {
buffer[pos++] = yearPrefix[0];
buffer[pos++] = yearPrefix[1];
return { "pos": pos };
}
}
return isValid;
},
cardinality: 1
},
{
validator: function (chrs, buffer, pos, strict, opts) {
var isValid = opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
if (!strict && !isValid) {
var yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs).toString().slice(0, 2);

isValid = opts.isInYearRange(chrs[0] + yearPrefix[1] + chrs[1], opts.yearrange.minyear, opts.yearrange.maxyear);
if (isValid) {
buffer[pos++] = yearPrefix[1];
return { "pos": pos };
}

yearPrefix = opts.determinebaseyear(opts.yearrange.minyear, opts.yearrange.maxyear, chrs).toString().slice(0, 2);
if (opts.isInYearRange(yearPrefix + chrs, opts.yearrange.minyear, opts.yearrange.maxyear)) {
var dayMonthValue = buffer.join('').substr(0, 6);
if (dayMonthValue != opts.leapday)
isValid = true;
else {
var year = parseInt(chrs, 10);//detect leap year
if (year % 4 === 0)
if (year % 100 === 0)
if (year % 400 === 0)
isValid = true;
else isValid = false;
else isValid = true;
else isValid = false;
}
} else isValid = false;
if (isValid) {
buffer[pos - 1] = yearPrefix[0];
buffer[pos++] = yearPrefix[1];
buffer[pos++] = chrs[0];
return { "pos": pos };
}
}
return isValid;
}, cardinality: 2
},
{
validator: function (chrs, buffer, pos, strict, opts) {
return opts.isInYearRange(chrs, opts.yearrange.minyear, opts.yearrange.maxyear);
}, cardinality: 3
}
]
}
},
insertMode: false,
autoUnmask: false
},
'yyyy-dd-mm': {
mask: "y-1-2",
placeholder: "yyyy-dd-mm",
leapday: "-29-02",
separator: '-',
alias: "yyyy/dd/mm"
},
'yyyy.dd.mm': {
mask: "y.1.2",
placeholder: "yyyy.dd.mm",
leapday: ".29.02",
separator: '.',
alias: "yyyy/dd/mm"
},