| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /*
- Input Mask plugin extensions
- http://github.com/RobinHerbots/jquery.inputmask
- Copyright (c) 2010 - 2014 Robin Herbots
- Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
- Version: 0.0.0
- Optional extensions on the jquery.inputmask base
- */
- (function ($) {
- //number aliases
- $.extend($.inputmask.defaults.aliases, {
- 'numeric': {
- mask: function (opts) {
- var mask = opts.prefix;
- mask += "[+]~{1," + opts.integerDigits + "}";
- mask += "[" + opts.radixPoint + "~{" + opts.digits + "}]";
- mask += opts.suffix;
- return mask;
- },
- placeholder: "",
- greedy: false,
- numericInput: false,
- isNumeric: false,
- digits: "2", //number of fractionalDigits
- groupSeparator: "",//",", // | "."
- radixPoint: ".",
- groupSize: 3,
- autoGroup: false,
- allowPlus: true,
- allowMinus: true,
- integerDigits: "20", //number of integerDigits
- defaultValue: "",
- prefix: "",
- suffix: "",
- postFormat: function (buffer, pos, reformatOnly, opts) {
- if (opts.groupSeparator == "") return pos;
- var cbuf = buffer.slice(),
- radixPos = $.inArray(opts.radixPoint, buffer);
- if (!reformatOnly) {
- cbuf.splice(pos, 0, "?"); //set position indicator
- }
- var bufVal = cbuf.join('');
- if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
- var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
- bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
- var radixSplit = bufVal.split(opts.radixPoint);
- bufVal = radixSplit[0];
- var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
- while (reg.test(bufVal)) {
- bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
- bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
- }
- if (radixSplit.length > 1)
- bufVal += opts.radixPoint + radixSplit[1];
- }
- buffer.length = bufVal.length; //align the length
- for (var i = 0, l = bufVal.length; i < l; i++) {
- buffer[i] = bufVal.charAt(i);
- }
- var newPos = $.inArray("?", buffer);
- if (!reformatOnly) buffer.splice(newPos, 1);
- return reformatOnly ? pos : newPos;
- },
- regex: {
- integerPart: function (opts) { return new RegExp('[-\+]?\\d+'); }
- },
- definitions: {
- '~': {
- validator: function (chrs, buffer, pos, strict, opts) {
- if (!strict && chrs === "-") {
- var matchRslt = buffer.join('').match(opts.regex.integerPart(opts));
- if (matchRslt.length > 0) {
- if (buffer[matchRslt.index] == "+") {
- buffer.splice(matchRslt.index, 1);
- return { "pos": matchRslt.index, "c": "-", "refreshFromBuffer": true, "caret": pos };
- } else if (buffer[matchRslt.index] == "-") {
- buffer.splice(matchRslt.index, 1);
- return { "refreshFromBuffer": true, "caret": pos - 1 };
- } else {
- return { "pos": matchRslt.index, "c": "-", "caret": pos + 1 };
- }
- }
- }
- var isValid = new RegExp("[0-9]").test(chrs);
- return isValid;
- },
- cardinality: 1,
- prevalidator: null
- },
- '+': {
- validator: function (chrs, buffer, pos, strict, opts) {
- var signed = "[";
- if (opts.allowMinus === true) signed += "-";
- if (opts.allowPlus === true) signed += "\+";
- signed += "]";
- var isValid = new RegExp(signed).test(chrs);
- return isValid;
- },
- cardinality: 1,
- prevalidator: null
- }
- },
- insertMode: true,
- autoUnmask: false
- },
- 'decimal': {
- mask: "~",
- placeholder: "",
- repeat: "*",
- greedy: false,
- numericInput: false,
- isNumeric: true,
- digits: "*", //number of fractionalDigits
- groupSeparator: "",//",", // | "."
- radixPoint: ".",
- groupSize: 3,
- autoGroup: false,
- allowPlus: true,
- allowMinus: true,
- //todo
- integerDigits: "*", //number of integerDigits
- defaultValue: "",
- prefix: "",
- suffix: "",
- postFormat: function (buffer, pos, reformatOnly, opts) {
- if (opts.groupSeparator == "") return pos;
- var cbuf = buffer.slice(),
- radixPos = $.inArray(opts.radixPoint, buffer);
- if (!reformatOnly) {
- cbuf.splice(pos, 0, "?"); //set position indicator
- }
- var bufVal = cbuf.join('');
- if (opts.autoGroup || (reformatOnly && bufVal.indexOf(opts.groupSeparator) != -1)) {
- var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
- bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), '');
- var radixSplit = bufVal.split(opts.radixPoint);
- bufVal = radixSplit[0];
- var reg = new RegExp('([-\+]?[\\d\?]+)([\\d\?]{' + opts.groupSize + '})');
- while (reg.test(bufVal)) {
- bufVal = bufVal.replace(reg, '$1' + opts.groupSeparator + '$2');
- bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
- }
- if (radixSplit.length > 1)
- bufVal += opts.radixPoint + radixSplit[1];
- }
- buffer.length = bufVal.length; //align the length
- for (var i = 0, l = bufVal.length; i < l; i++) {
- buffer[i] = bufVal.charAt(i);
- }
- var newPos = $.inArray("?", buffer);
- if (!reformatOnly) buffer.splice(newPos, 1);
- return reformatOnly ? pos : newPos;
- },
- regex: {
- number: function (opts) {
- var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
- var digitExpression = isNaN(opts.digits) ? opts.digits : '{0,' + opts.digits + '}';
- var integerExpression = isNaN(opts.integerDigits) ? opts.integerDigits : '{1,' + opts.integerDigits + '}';
- var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
- var currentRegExp = "^" + signedExpression + "\\d" + integerExpression + "(" + escapedRadixPoint + "\\d" + digitExpression + ")?$";
- return new RegExp(currentRegExp);
- }
- },
- onKeyDown: function (e, buffer, opts) {
- var $input = $(this), input = this;
- if (e.keyCode == opts.keyCode.TAB) {
- var radixPosition = $.inArray(opts.radixPoint, buffer);
- if (radixPosition != -1) {
- var masksets = $input.data('_inputmask')['masksets'];
- var activeMasksetIndex = $input.data('_inputmask')['activeMasksetIndex'];
- for (var i = 1; i <= opts.digits && i < opts.getMaskLength(masksets[activeMasksetIndex]["_buffer"], opts.greedy, opts.repeat, buffer, opts) ; i++) {
- if (buffer[radixPosition + i] == undefined || buffer[radixPosition + i] == "") buffer[radixPosition + i] = "0";
- }
- return { "refreshFromBuffer": true };
- }
- } else if (e.keyCode == opts.keyCode.DELETE || e.keyCode == opts.keyCode.BACKSPACE) {
- opts.postFormat(buffer, 0, true, opts);
- input._valueSet(buffer.join(''));
- return { "refreshFromBuffer": true };
- }
- },
- definitions: {
- '~': { //real number
- validator: function (chrs, buffer, pos, strict, opts) {
- var iopts = $.extend({}, opts, { digits: strict ? "*" : opts.digits });
- if (chrs == "") return false;
- if (!strict && pos <= 1 && buffer[0] === '0' && new RegExp("[\\d-]").test(chrs) && buffer.join('').length == 1) { //handle first char
- buffer[0] = "";
- return { "pos": 0 };
- }
- var cbuf = strict ? buffer.slice(0, pos) : buffer.slice();
- cbuf.splice(pos, 0, chrs);
- var bufferStr = cbuf.join('');
- //strip groupseparator
- var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
- bufferStr = bufferStr.replace(new RegExp(escapedGroupSeparator, "g"), '');
- if (strict && bufferStr.lastIndexOf(opts.radixPoint) == bufferStr.length - 1) {
- var escapedRadixPoint = $.inputmask.escapeRegex.call(this, opts.radixPoint);
- bufferStr = bufferStr.replace(new RegExp(escapedRadixPoint, "g"), '');
- }
- if (!strict && bufferStr == "") return false;
- var isValid = opts.regex.number(iopts).test(bufferStr);
- if (!isValid) {
- //let's help the regex a bit
- bufferStr += "0";
- isValid = opts.regex.number(iopts).test(bufferStr);
- if (!isValid) {
- //make a valid group
- var lastGroupSeparator = bufferStr.lastIndexOf(opts.groupSeparator);
- for (var i = bufferStr.length - lastGroupSeparator; i <= 3; i++) {
- bufferStr += "0";
- }
- isValid = opts.regex.number(iopts).test(bufferStr);
- if (!isValid && !strict) {
- if (chrs == opts.radixPoint) {
- isValid = opts.regex.number(iopts).test("0" + bufferStr + "0");
- if (isValid) {
- buffer[pos] = "0";
- pos++;
- return { "pos": pos };
- }
- }
- }
- }
- }
- if (isValid != false && !strict && chrs != opts.radixPoint) {
- var newPos = opts.postFormat(buffer, pos, (chrs == "-" || chrs == "+") ? true : false, opts);
- return { "pos": newPos, "refreshFromBuffer": true };
- }
- return isValid;
- },
- cardinality: 1,
- prevalidator: null
- }
- },
- insertMode: true,
- autoUnmask: false
- },
- 'integer': {
- regex: {
- number: function (opts) {
- var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
- var signedExpression = opts.allowPlus || opts.allowMinus ? "[" + (opts.allowPlus ? "\+" : "") + (opts.allowMinus ? "-" : "") + "]?" : "";
- return new RegExp("^" + signedExpression + "(\\d+|\\d{1," + opts.groupSize + "}((" + escapedGroupSeparator + "\\d{" + opts.groupSize + "})?)+)$");
- }
- },
- alias: "decimal"
- }
- });
- })(jQuery);
|