added input masks

This commit is contained in:
root
2019-06-14 01:22:46 -04:00
parent 3d3bcff09e
commit 0eda7e9762
86 changed files with 20308 additions and 28 deletions

View File

@@ -0,0 +1,323 @@
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) Robin Herbots
Licensed under the MIT license
*/
var Inputmask = require("../inputmask"), $ = Inputmask.dependencyLib,
//supported codes for formatting
//http://blog.stevenlevithan.com/archives/date-time-format
//https://docs.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings?view=netframework-4.7
formatCode = { //regex, valueSetter, type, displayformatter
d: ["[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", Date.prototype.getDate], //Day of the month as digits; no leading zero for single-digit days.
dd: ["0[1-9]|[12][0-9]|3[01]", Date.prototype.setDate, "day", function () {
return pad(Date.prototype.getDate.call(this), 2);
}], //Day of the month as digits; leading zero for single-digit days.
ddd: [""], //Day of the week as a three-letter abbreviation.
dddd: [""], //Day of the week as its full name.
m: ["[1-9]|1[012]", Date.prototype.setMonth, "month", function () {
return Date.prototype.getMonth.call(this) + 1;
}], //Month as digits; no leading zero for single-digit months.
mm: ["0[1-9]|1[012]", Date.prototype.setMonth, "month", function () {
return pad(Date.prototype.getMonth.call(this) + 1, 2);
}], //Month as digits; leading zero for single-digit months.
mmm: [""], //Month as a three-letter abbreviation.
mmmm: [""], //Month as its full name.
yy: ["[0-9]{2}", Date.prototype.setFullYear, "year", function () {
return pad(Date.prototype.getFullYear.call(this), 2);
}], //Year as last two digits; leading zero for years less than 10.
yyyy: ["[0-9]{4}", Date.prototype.setFullYear, "year", function () {
return pad(Date.prototype.getFullYear.call(this), 4);
}],
h: ["[1-9]|1[0-2]", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no leading zero for single-digit hours (12-hour clock).
hh: ["0[1-9]|1[0-2]", Date.prototype.setHours, "hours", function () {
return pad(Date.prototype.getHours.call(this), 2);
}], //Hours; leading zero for single-digit hours (12-hour clock).
hhh: ["[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no limit
H: ["1?[0-9]|2[0-3]", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no leading zero for single-digit hours (24-hour clock).
HH: ["0[0-9]|1[0-9]|2[0-3]", Date.prototype.setHours, "hours", function () {
return pad(Date.prototype.getHours.call(this), 2);
}], //Hours; leading zero for single-digit hours (24-hour clock).
HHH: ["[0-9]+", Date.prototype.setHours, "hours", Date.prototype.getHours], //Hours; no limit
M: ["[1-5]?[0-9]", Date.prototype.setMinutes, "minutes", Date.prototype.getMinutes], //Minutes; no leading zero for single-digit minutes. Uppercase M unlike CF timeFormat's m to avoid conflict with months.
MM: ["0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]", Date.prototype.setMinutes, "minutes", function () {
return pad(Date.prototype.getMinutes.call(this), 2);
}], //Minutes; leading zero for single-digit minutes. Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.
s: ["[1-5]?[0-9]", Date.prototype.setSeconds, "seconds", Date.prototype.getSeconds], //Seconds; no leading zero for single-digit seconds.
ss: ["0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]", Date.prototype.setSeconds, "seconds", function () {
return pad(Date.prototype.getSeconds.call(this), 2);
}], //Seconds; leading zero for single-digit seconds.
l: ["[0-9]{3}", Date.prototype.setMilliseconds, "milliseconds", function () {
return pad(Date.prototype.getMilliseconds.call(this), 3);
}], //Milliseconds. 3 digits.
L: ["[0-9]{2}", Date.prototype.setMilliseconds, "milliseconds", function () {
return pad(Date.prototype.getMilliseconds.call(this), 2);
}], //Milliseconds. 2 digits.
t: ["[ap]"], //Lowercase, single-character time marker string: a or p.
tt: ["[ap]m"], //two-character time marker string: am or pm.
T: ["[AP]"], //single-character time marker string: A or P.
TT: ["[AP]M"], //two-character time marker string: AM or PM.
Z: [""], //US timezone abbreviation, e.g. EST or MDT. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g. GMT-0500
o: [""], //GMT/UTC timezone offset, e.g. -0500 or +0230.
S: [""] //The date's ordinal suffix (st, nd, rd, or th).
},
formatAlias = {
isoDate: "yyyy-mm-dd", //2007-06-09
isoTime: "HH:MM:ss", //17:46:21
isoDateTime: "yyyy-mm-dd'T'HH:MM:ss", //2007-06-09T17:46:21
isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'" //2007-06-09T22:46:21Z
};
function getTokenizer(opts) {
if (!opts.tokenizer) {
var tokens = [];
for (var ndx in formatCode) {
if (tokens.indexOf(ndx[0]) === -1) {
tokens.push(ndx[0]);
}
}
opts.tokenizer = "(" + tokens.join("+|") + ")+?|.";
opts.tokenizer = new RegExp(opts.tokenizer, "g");
}
return opts.tokenizer;
}
function isValidDate(dateParts, currentResult) {
return !isFinite(dateParts.rawday)
|| (dateParts.day == "29" && !isFinite(dateParts.rawyear))
|| new Date(dateParts.date.getFullYear(), isFinite(dateParts.rawmonth) ? dateParts.month : dateParts.date.getMonth() + 1, 0).getDate() >= dateParts.day
? currentResult
: false; //take corrective action if possible
}
function isDateInRange(dateParts, opts) {
var result = true;
if (opts.min) {
if (dateParts["rawyear"]) {
var rawYear = dateParts["rawyear"].replace(/[^0-9]/g, ""),
minYear = opts.min.year.substr(0, rawYear.length);
result = minYear <= rawYear;
}
if (dateParts["year"] === dateParts["rawyear"]) {
if (opts.min.date.getTime() === opts.min.date.getTime()) {
result = opts.min.date.getTime() <= dateParts.date.getTime();
}
}
}
if (result && opts.max && opts.max.date.getTime() === opts.max.date.getTime()) {
result = opts.max.date.getTime() >= dateParts.date.getTime();
}
return result;
}
//parse the given format and return a mask pattern
//when a dateObjValue is passed a datestring in the requested format is returned
function parse(format, dateObjValue, opts, raw) {
//parse format to regex string
var mask = "", match;
while ((match = getTokenizer(opts).exec(format))) {
if (dateObjValue === undefined) {
if (formatCode[match[0]]) {
mask += "(" + formatCode[match[0]][0] + ")";
} else {
switch (match[0]) {
case "[":
mask += "(";
break;
case "]":
mask += ")?";
break;
default:
mask += Inputmask.escapeRegex(match[0]);
}
}
} else {
if (formatCode[match[0]]) {
if (raw !== true && formatCode[match[0]][3]) {
var getFn = formatCode[match[0]][3];
mask += getFn.call(dateObjValue.date);
} else if (formatCode[match[0]][2]) {
mask += dateObjValue["raw" + formatCode[match[0]][2]];
} else {
mask += match[0];
}
} else {
mask += match[0];
}
}
}
return mask;
}
//padding function
function pad(val, len) {
val = String(val);
len = len || 2;
while (val.length < len) val = "0" + val;
return val;
}
function analyseMask(maskString, format, opts) {
var dateObj = {"date": new Date(1, 0, 1)}, targetProp, mask = maskString, match, dateOperation;
function extendProperty(value) {
var correctedValue = value.replace(/[^0-9]/g, "0");
// if (correctedValue != value) { //only do correction on incomplete values
// //determine best validation match
// var enteredPart = value.replace(/[^0-9]/g, ""),
// enteredPartIndex = value.indexOf(enteredPart),
// minPart = (opts.min && opts.min[targetProp] || value).slice(enteredPartIndex, enteredPartIndex + enteredPart.length),
// maxPart = (opts.max && opts.max[targetProp] || value).slice(enteredPartIndex, enteredPartIndex + enteredPart.length),
// correctedPart = enteredPart < minPart ? minPart : (enteredPart > maxPart ? maxPart : correctedValue.slice(enteredPartIndex, enteredPartIndex + enteredPart.length));
// correctedValue = correctedValue.split("");
// correctedValue.splice(enteredPartIndex, 1, correctedPart);
// correctedValue = correctedValue.join("");
// }
return correctedValue;
}
function setValue(dateObj, value, opts) {
dateObj[targetProp] = extendProperty(value);
dateObj["raw" + targetProp] = value;
if (dateOperation !== undefined) {
dateOperation.call(dateObj.date, targetProp == "month" ? parseInt(dateObj[targetProp]) - 1 : dateObj[targetProp]);
}
}
if (typeof mask === "string") {
while ((match = getTokenizer(opts).exec(format))) {
var value = mask.slice(0, match[0].length);
if (formatCode.hasOwnProperty(match[0])) {
// targetValidator = formatCode[match[0]][0];
targetProp = formatCode[match[0]][2];
dateOperation = formatCode[match[0]][1];
setValue(dateObj, value, opts);
}
mask = mask.slice(value.length);
}
return dateObj;
} else if (mask && typeof mask === "object" && mask.hasOwnProperty("date")) {
return mask;
}
return undefined;
}
Inputmask.extendAliases({
"datetime": {
mask: function (opts) {
//localize
formatCode.S = opts.i18n.ordinalSuffix.join("|");
opts.inputFormat = formatAlias[opts.inputFormat] || opts.inputFormat; //resolve possible formatAlias
opts.displayFormat = formatAlias[opts.displayFormat] || opts.displayFormat || opts.inputFormat; //resolve possible formatAlias
opts.outputFormat = formatAlias[opts.outputFormat] || opts.outputFormat || opts.inputFormat; //resolve possible formatAlias
opts.placeholder = opts.placeholder !== "" ? opts.placeholder : opts.inputFormat.replace(/[[\]]/, "");
opts.regex = parse(opts.inputFormat, undefined, opts);
// console.log(opts.regex);
return null; //migrate to regex mask
},
placeholder: "", //set default as none (~ auto); when a custom placeholder is passed it will be used
inputFormat: "isoDateTime", //format used to input the date
displayFormat: undefined, //visual format when the input looses focus
outputFormat: undefined, //unmasking format
min: null, //needs to be in the same format as the inputfornat
max: null, //needs to be in the same format as the inputfornat,
// Internationalization strings
i18n: {
dayNames: [
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun",
"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"
],
monthNames: [
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
],
ordinalSuffix: ["st", "nd", "rd", "th"]
},
preValidation: function (buffer, pos, c, isSelection, opts, maskset) {
var calcPos = 0, targetMatch, match;
if (isNaN(c) && buffer[pos] !== c) {
while ((match = getTokenizer(opts).exec(opts.inputFormat))) {
calcPos += match[0].length;
if (calcPos >= pos) {
targetMatch = match;
match = getTokenizer(opts).exec(opts.inputFormat);
break;
}
}
if (match && match[0] === c && targetMatch[0].length > 1) {
buffer[pos] = buffer[pos - 1];
buffer[pos - 1] = "0";
return {
fuzzy: true,
buffer: buffer,
refreshFromBuffer: {start: pos - 1, end: pos + 1},
pos: pos + 1
};
}
}
return true;
},
postValidation: function (buffer, pos, currentResult, opts) {
opts.min = analyseMask(opts.min, opts.inputFormat, opts);
opts.max = analyseMask(opts.max, opts.inputFormat, opts);
if (currentResult.fuzzy) {
buffer = currentResult.buffer;
pos = currentResult.pos;
}
var result = currentResult, dateParts = analyseMask(buffer.join(""), opts.inputFormat, opts);
if (result && dateParts.date.getTime() === dateParts.date.getTime()) { //check for a valid date ~ an invalid date returns NaN which isn't equal
result = isValidDate(dateParts, result);
result = result && isDateInRange(dateParts, opts);
}
if (pos && result && currentResult.pos !== pos) {
return {
buffer: parse(opts.inputFormat, dateParts, opts),
refreshFromBuffer: {start: pos, end: currentResult.pos}
};
}
return result;
},
onKeyDown: function (e, buffer, caretPos, opts) {
var input = this;
if (e.ctrlKey && e.keyCode === Inputmask.keyCode.RIGHT) {
var today = new Date(), match, date = "";
while ((match = getTokenizer(opts).exec(opts.inputFormat))) {
if (match[0].charAt(0) === "d") {
date += pad(today.getDate(), match[0].length);
} else if (match[0].charAt(0) === "m") {
date += pad((today.getMonth() + 1), match[0].length);
} else if (match[0] === "yyyy") {
date += today.getFullYear().toString();
} else if (match[0].charAt(0) === "y") {
date += pad(today.getYear(), match[0].length);
}
}
input.inputmask._valueSet(date);
$(input).trigger("setvalue");
}
},
onUnMask: function (maskedValue, unmaskedValue, opts) {
return unmaskedValue ? parse(opts.outputFormat, analyseMask(maskedValue, opts.inputFormat, opts), opts, true) : unmaskedValue;
},
casing: function (elem, test, pos, validPositions) {
if (test.nativeDef.indexOf("[ap]") == 0) return elem.toLowerCase();
if (test.nativeDef.indexOf("[AP]") == 0) return elem.toUpperCase();
return elem;
},
insertMode: false,
shiftPositions: false
}
});
module.exports = Inputmask;

View File

@@ -0,0 +1,92 @@
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) Robin Herbots
Licensed under the MIT license
*/
var Inputmask = require("../inputmask");
//extra definitions
Inputmask.extendDefinitions({
"A": {
validator: "[A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
casing: "upper" //auto uppercasing
},
"&": { //alfanumeric uppercasing
validator: "[0-9A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5]",
casing: "upper"
},
"#": { //hexadecimal
validator: "[0-9A-Fa-f]",
casing: "upper"
}
});
Inputmask.extendAliases({
"cssunit": {
regex: "[+-]?[0-9]+\\.?([0-9]+)?(px|em|rem|ex|%|in|cm|mm|pt|pc)"
},
"url": { //needs update => https://en.wikipedia.org/wiki/URL
regex: "(https?|ftp)//.*",
autoUnmask: false
},
"ip": { //ip-address mask
mask: "i[i[i]].i[i[i]].i[i[i]].i[i[i]]",
definitions: {
"i": {
validator: function (chrs, maskset, pos, strict, opts) {
if (pos - 1 > -1 && maskset.buffer[pos - 1] !== ".") {
chrs = maskset.buffer[pos - 1] + chrs;
if (pos - 2 > -1 && maskset.buffer[pos - 2] !== ".") {
chrs = maskset.buffer[pos - 2] + chrs;
} else chrs = "0" + chrs;
} else chrs = "00" + chrs;
return new RegExp("25[0-5]|2[0-4][0-9]|[01][0-9][0-9]").test(chrs);
}
}
},
onUnMask: function (maskedValue, unmaskedValue, opts) {
return maskedValue;
},
inputmode: "numeric",
},
"email": {
//https://en.wikipedia.org/wiki/Domain_name#Domain_name_space
//https://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names
//should be extended with the toplevel domains at the end
mask: "*{1,64}[.*{1,64}][.*{1,64}][.*{1,63}]@-{1,63}.-{1,63}[.-{1,63}][.-{1,63}]",
greedy: false,
casing: "lower",
onBeforePaste: function (pastedValue, opts) {
pastedValue = pastedValue.toLowerCase();
return pastedValue.replace("mailto:", "");
},
definitions: {
"*": {
validator: "[0-9\uFF11-\uFF19A-Za-z\u0410-\u044F\u0401\u0451\u00C0-\u00FF\u00B5!#$%&'*+/=?^_`{|}~-]"
},
"-": {
validator: "[0-9A-Za-z-]"
}
},
onUnMask: function (maskedValue, unmaskedValue, opts) {
return maskedValue;
},
inputmode: "email"
},
"mac": {
mask: "##:##:##:##:##:##"
},
//https://en.wikipedia.org/wiki/Vehicle_identification_number
// see issue #1199
"vin": {
mask: "V{13}9{4}",
definitions: {
"V": {
validator: "[A-HJ-NPR-Za-hj-npr-z\\d]",
casing: "upper"
}
},
clearIncomplete: true,
autoUnmask: true
}
});
module.exports = Inputmask;

View File

@@ -0,0 +1,454 @@
/*
Input Mask plugin extensions
http://github.com/RobinHerbots/jquery.inputmask
Copyright (c) Robin Herbots
Licensed under the MIT license
*/
var Inputmask = require("../inputmask"), $ = Inputmask.dependencyLib;
function autoEscape(txt, opts) {
var escapedTxt = "";
for (var i = 0; i < txt.length; i++) {
if (Inputmask.prototype.definitions[txt.charAt(i)] ||
opts.definitions[txt.charAt(i)] ||
opts.optionalmarker.start === txt.charAt(i) ||
opts.optionalmarker.end === txt.charAt(i) ||
opts.quantifiermarker.start === txt.charAt(i) ||
opts.quantifiermarker.end === txt.charAt(i) ||
opts.groupmarker.start === txt.charAt(i) ||
opts.groupmarker.end === txt.charAt(i) ||
opts.alternatormarker === txt.charAt(i)) {
escapedTxt += "\\" + txt.charAt(i);
} else {
escapedTxt += txt.charAt(i);
}
}
return escapedTxt;
}
function alignDigits(buffer, digits, opts) {
if (digits > 0 && !opts.digitsOptional && buffer.length > 0) {
var radixPosition = $.inArray(opts.radixPoint, buffer);
if (radixPosition === -1) {
buffer.push(opts.radixPoint);
radixPosition = buffer.length - 1;
}
for (var i = 1; i <= digits; i++) {
buffer[radixPosition + i] = buffer[radixPosition + i] || "0";
}
}
return buffer;
}
function findValidator(symbol, maskset) {
var posNdx = 0;
if (symbol === "+") {
for (posNdx in maskset.validPositions) ;
posNdx = parseInt(posNdx);
}
for (var tstNdx in maskset.tests) {
tstNdx = parseInt(tstNdx);
if (tstNdx >= posNdx) {
for (var ndx = 0, ndxl = maskset.tests[tstNdx].length; ndx < ndxl; ndx++) {
if ((maskset.validPositions[tstNdx] === undefined || symbol === "-") && maskset.tests[tstNdx][ndx].match.def === symbol) {
return tstNdx + ((maskset.validPositions[tstNdx] !== undefined && symbol !== "-") ? 1 : 0);
}
}
}
}
return posNdx;
}
function findValid(symbol, maskset) {
var ret = -1;
$.each(maskset.validPositions, function (ndx, tst) {
if (tst.match.def === symbol) {
ret = parseInt(ndx);
return false;
}
});
return ret;
}
function parseMinMaxOptions(opts) {
if (opts.parseMinMaxOptions === undefined) {
// convert min and max options
if (opts.min !== null) {
opts.min = opts.min.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
if (opts.radixPoint === ",") opts.min = opts.min.replace(opts.radixPoint, ".");
opts.min = isFinite(opts.min) ? parseFloat(opts.min) : NaN;
if (isNaN(opts.min)) opts.min = Number.MIN_VALUE;
}
if (opts.max !== null) {
opts.max = opts.max.toString().replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
if (opts.radixPoint === ",") opts.max = opts.max.replace(opts.radixPoint, ".");
opts.max = isFinite(opts.max) ? parseFloat(opts.max) : NaN;
if (isNaN(opts.max)) opts.max = Number.MAX_VALUE;
}
opts.parseMinMaxOptions = "done";
}
}
function genMask(opts) {
opts.repeat = 0;
//treat equal separator and radixpoint
if (opts.groupSeparator === opts.radixPoint && opts.digits && opts.digits !== "0") {
if (opts.radixPoint === ".") {
opts.groupSeparator = ",";
} else if (opts.radixPoint === ",") {
opts.groupSeparator = ".";
} else {
opts.groupSeparator = "";
}
}
//prevent conflict with default skipOptionalPartCharacter
if (opts.groupSeparator === " ") {
opts.skipOptionalPartCharacter = undefined;
}
//enforce placeholder to single
if (opts.placeholder.length > 1) {
opts.placeholder = opts.placeholder.charAt(0);
}
//only allow radixfocus when placeholder = 0
if (opts.positionCaretOnClick === "radixFocus" && opts.placeholder === "") {
opts.positionCaretOnClick = "lvp";
}
var decimalDef = "0";
if (opts.numericInput === true && opts.__financeInput === undefined) { //finance people input style
decimalDef = "1";
opts.positionCaretOnClick = opts.positionCaretOnClick === "radixFocus" ? "lvp" : opts.positionCaretOnClick;
// opts.digitsOptional = false;
if (isNaN(opts.digits)) opts.digits = 2;
opts._radixDance = false;
} else {
opts.__financeInput = false; //needed to keep original selection when remasking
opts.numericInput = true;
}
var mask = "[+]", altMask;
mask += autoEscape(opts.prefix, opts);
if (opts.groupSeparator !== "") {
mask += opts._mask(opts);
} else {
mask += "9{+}";
}
if (opts.digits !== undefined) {
var dq = opts.digits.toString().split(",");
if (isFinite(dq[0]) && dq[1] && isFinite(dq[1])) {
mask += opts.radixPoint + decimalDef + "{" + opts.digits + "}";
} else if (isNaN(opts.digits) || parseInt(opts.digits) > 0) {
if (opts.digitsOptional) {
altMask = mask + opts.radixPoint + decimalDef + "{0," + opts.digits + "}";
// mask += "[" + opts.radixPoint + "]";
opts.keepStatic = true;
} else {
mask += opts.radixPoint + decimalDef + "{" + opts.digits + "}";
}
}
}
mask += autoEscape(opts.suffix, opts);
mask += "[-]";
if (altMask) {
mask = [(altMask + autoEscape(opts.suffix, opts) + "[-]"), mask];
}
opts.greedy = false; //enforce greedy false
parseMinMaxOptions(opts);
return mask;
}
function hanndleRadixDance(pos, c, radixPos, opts) {
if (opts._radixDance && opts.numericInput && c !== opts.negationSymbol.back) {
if (pos <= radixPos && (radixPos > 0 || c == opts.radixPoint)) {
pos -= 1;
}
}
return pos;
}
function decimalValidator(chrs, maskset, pos, strict, opts) {
var radixPos = maskset.buffer.indexOf(opts.radixPoint),
result = radixPos !== -1 && new RegExp("[0-9\uFF11-\uFF19]").test(chrs);
if (opts._radixDance && result && maskset.validPositions[radixPos] == undefined) {
return {
insert: {
pos: radixPos === pos ? radixPos + 1 : radixPos,
c: opts.radixPoint
},
pos: pos
};
}
return result;
}
//number aliases
Inputmask.extendAliases({
"numeric": {
mask: genMask,
_mask: function (opts) {
return "(" + opts.groupSeparator + "999){+|1}";
},
placeholder: "0",
greedy: false,
digits: "*", //number of fractionalDigits
digitsOptional: true,
enforceDigitsOnBlur: false,
radixPoint: ".",
positionCaretOnClick: "radixFocus",
_radixDance: true,
groupSeparator: "",
allowMinus: true,
negationSymbol: {
front: "-", //"("
back: "" //")"
},
prefix: "",
suffix: "",
rightAlign: true,
min: null, //minimum value
max: null, //maximum value
step: 1,
insertMode: true,
autoUnmask: false,
unmaskAsNumber: false,
inputmode: "numeric",
definitions: {
"0": {
validator: decimalValidator
},
"1": {
validator: decimalValidator,
definitionSymbol: "*"
},
"+": {
validator: function (chrs, maskset, pos, strict, opts) {
return (opts.allowMinus && (chrs === "-" || chrs === opts.negationSymbol.front));
}
},
"-": {
validator: function (chrs, maskset, pos, strict, opts) {
return (opts.allowMinus && chrs === opts.negationSymbol.back);
}
}
},
preValidation: function (buffer, pos, c, isSelection, opts, maskset) {
var radixPos = $.inArray(opts.radixPoint, buffer);
pos = hanndleRadixDance(pos, c, radixPos, opts);
if (c === "-" || c === opts.negationSymbol.front) {
if (opts.allowMinus !== true) return false;
var isNegative = false,
front = findValid("+", maskset), back = findValid("-", maskset);
if (front !== -1) {
isNegative = [front, back];
}
return isNegative !== false ? {
remove: isNegative,
caret: radixPos > pos ? pos + 1 : pos
} : {
insert: [
{pos: findValidator("+", maskset), c: opts.negationSymbol.front, fromIsValid: true},
{pos: findValidator("-", maskset), c: opts.negationSymbol.back, fromIsValid: undefined}],
caret: radixPos > pos ? pos + 1 : pos
};
}
if (radixPos !== -1 && (opts._radixDance === true && isSelection === false && c === opts.radixPoint && (opts.digits !== undefined && (isNaN(opts.digits) || parseInt(opts.digits) > 0)) && radixPos !== pos)) {
return {
"caret": opts._radixDance && pos === radixPos - 1 ? radixPos + 1 : radixPos
};
}
return {rewritePosition: pos};
},
postValidation: function (buffer, pos, currentResult, opts) {
if (opts.min !== null || opts.max !== null) {
var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
unmaskAsNumber: true
}));
if (opts.min !== null && unmasked < opts.min && unmasked.toString().length >= opts.min.toString().length) {
return false;
}
if (opts.max !== null && unmasked > opts.max) {
return false;
}
}
return currentResult;
},
onUnMask: function (maskedValue, unmaskedValue, opts) {
if (unmaskedValue === "" && opts.nullable === true) {
return unmaskedValue;
}
var processValue = maskedValue.replace(opts.prefix, "");
processValue = processValue.replace(opts.suffix, "");
processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator), "g"), "");
if (opts.placeholder.charAt(0) !== "") {
processValue = processValue.replace(new RegExp(opts.placeholder.charAt(0), "g"), "0");
}
if (opts.unmaskAsNumber) {
if (opts.radixPoint !== "" && processValue.indexOf(opts.radixPoint) !== -1) processValue = processValue.replace(Inputmask.escapeRegex.call(this, opts.radixPoint), ".");
processValue = processValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
processValue = processValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
return Number(processValue);
}
return processValue;
},
isComplete: function (buffer, opts) {
var maskedValue = (opts.numericInput ? buffer.slice().reverse() : buffer).join("");
maskedValue = maskedValue.replace(new RegExp("^" + Inputmask.escapeRegex(opts.negationSymbol.front)), "-");
maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.negationSymbol.back) + "$"), "");
maskedValue = maskedValue.replace(opts.prefix, "");
maskedValue = maskedValue.replace(opts.suffix, "");
maskedValue = maskedValue.replace(new RegExp(Inputmask.escapeRegex(opts.groupSeparator) + "([0-9]{3})", "g"), "$1");
if (opts.radixPoint === ",") maskedValue = maskedValue.replace(Inputmask.escapeRegex(opts.radixPoint), ".");
return isFinite(maskedValue);
},
onBeforeMask: function (initialValue, opts) {
var radixPoint = opts.radixPoint || ",";
if ((typeof initialValue == "number" || opts.inputType === "number") && radixPoint !== "") {
initialValue = initialValue.toString().replace(".", radixPoint);
}
var valueParts = initialValue.split(radixPoint),
integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "";
initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
var digits = 0;
if (radixPoint !== "") {
digits = decimalPart.length;
if (decimalPart !== "") {
var digitsFactor = Math.pow(10, digits || 1);
if (isFinite(opts.digits)) {
digits = parseInt(opts.digits);
digitsFactor = Math.pow(10, digits);
}
//make the initialValue a valid javascript number for the parsefloat
initialValue = initialValue.replace(Inputmask.escapeRegex(radixPoint), ".");
if (isFinite(initialValue)) {
initialValue = Math.round(parseFloat(initialValue) * digitsFactor) / digitsFactor;
}
initialValue = initialValue.toString().replace(".", radixPoint);
}
}
//this needs to be in a separate part and not directly in decimalPart to allow rounding
if (opts.digits === 0 && initialValue.indexOf(Inputmask.escapeRegex(radixPoint)) !== -1) {
initialValue = initialValue.substring(0, initialValue.indexOf(Inputmask.escapeRegex(radixPoint)));
}
return alignDigits(initialValue.toString().split(""), digits, opts).join("");
},
onBeforeWrite: function (e, buffer, caretPos, opts) {
var result;
//check leading zeros
var numberMatches = new RegExp("^" + (opts.negationSymbol.front != "" ? Inputmask.escapeRegex(opts.negationSymbol.front) + "?" : "") + Inputmask.escapeRegex(opts.prefix) + "(?<number>.*)" + Inputmask.escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? Inputmask.escapeRegex(opts.negationSymbol.back) + "?" : "") + "$").exec(buffer.slice().reverse().join("")),
number = numberMatches ? numberMatches.groups.number : "";
if (false && number) {
number = number.split(opts.radixPoint.charAt(0))[0];
var leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
if (leadingzeroes[0].length > 1 || leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length) {
var buf = buffer.slice().reverse(), caretNdx = buf.join("").indexOf(leadingzeroes[0]);
buf.splice(caretNdx, leadingzeroes[0].length);
var newCaretPos = buf.length - caretNdx;
result = {
refreshFromBuffer: true,
buffer: buf.reverse(),
caret: caretPos < newCaretPos ? caretPos : newCaretPos
};
}
}
if (e) {
switch (e.type) {
case "blur":
case "checkval":
if (opts.radixPoint !== "" && buffer[0] === opts.radixPoint) {
if (result && result.buffer) {
result.buffer.shift();
} else {
buffer.shift();
result =
{refreshFromBuffer: true, buffer: buffer};
}
}
}
}
return result;
},
onKeyDown: function (e, buffer, caretPos, opts) {
var $input = $(this);
if (e.ctrlKey) {
switch (e.keyCode) {
case Inputmask.keyCode.UP:
this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) + parseInt(opts.step));
$input.trigger("setvalue");
return false;
case Inputmask.keyCode.DOWN:
this.inputmask.__valueSet.call(this, parseFloat(this.inputmask.unmaskedvalue()) - parseInt(opts.step));
$input.trigger("setvalue");
return false;
}
}
if (e.keyCode === Inputmask.keyCode.DELETE || e.keyCode === Inputmask.keyCode.BACKSPACE || e.keyCode === Inputmask.keyCode.BACKSPACE_SAFARI) {
if (opts._radixDance === true && !opts.digitsOptional) {
var radixPos = $.inArray(opts.radixPoint, buffer);
if (radixPos !== -1 && (caretPos < radixPos || (e.keyCode === Inputmask.keyCode.DELETE && caretPos === radixPos))) {
if (e.keyCode === Inputmask.keyCode.BACKSPACE || e.keyCode === Inputmask.keyCode.BACKSPACE_SAFARI) {
caretPos++;
}
var bffr = buffer.slice().reverse();
bffr.splice(bffr.length - caretPos, 1);
$input.trigger("setvalue", [alignDigits(bffr, opts.digits, opts).join(""), caretPos]);
return false;
}
}
}
}
},
"currency": {
prefix: "$ ",
groupSeparator: ",",
alias: "numeric",
placeholder: "0",
digits: 2,
digitsOptional: false
},
"decimal": {
alias: "numeric"
},
"integer": {
alias: "numeric",
digits: 0
},
"percentage": {
alias: "integer",
min: 0,
max: 100,
suffix: " %",
allowMinus: false
},
"indianns": { //indian numbering system
alias: "numeric",
_mask: function (opts) {
return "(" + opts.groupSeparator + "99){*|1}(" + opts.groupSeparator + "999){1|1}";
},
groupSeparator: ",",
radixPoint: ".",
placeholder: "0",
digits: 2,
digitsOptional: false
}
});
module.exports = Inputmask;