Browse Source

update pre & post validation conditions

Robin Herbots 6 years ago
parent
commit
d45af5c1ce

+ 2 - 2
README.md

@@ -1009,10 +1009,10 @@ $(selector).inputmask({
 ```
 
 ### postValidation
-Hook to postValidate the result from isValid.  Usefull for validating the entry as a whole.  Args => buffer, pos, currentResult, opts<br>Return => true|false|command object
+Hook to postValidate the result from isValid.  Usefull for validating the entry as a whole.  Args => buffer, pos, currentResult, opts, maskset, strict<br>Return => true|false|command object
 
 ### preValidation
-Hook to preValidate the input.  Useful for validating regardless the definition. Args => buffer, pos, char, isSelection, opts, maskset, caretPos => return true/false/command object
+Hook to preValidate the input.  Useful for validating regardless the definition. Args => buffer, pos, char, isSelection, opts, maskset, caretPos, strict => return true/false/command object
 When return true, the normal validation kicks in, otherwise it is skipped.
 
 ### staticDefinitionSymbol

+ 1 - 1
bower.json

@@ -1,6 +1,6 @@
 {
   "name": "inputmask",
-  "version": "5.0.0-beta.262",
+  "version": "5.0.0-beta.263",
   "main": [
 	  "./index.js",
     "./css/inputmask.css"

+ 1 - 1
composer.json

@@ -1,7 +1,7 @@
 {
   "name": "robinherbots/inputmask",
   "description": "Inputmask is a javascript library which creates an input mask.  Inputmask can run against vanilla javascript, jQuery and jqlite.",
-  "version": "5.0.0-beta.262",
+  "version": "5.0.0-beta.263",
   "type": "library",
   "keywords": ["jquery", "plugins", "input", "form", "inputmask", "mask"],
   "homepage": "http://robinherbots.github.io/Inputmask",

File diff suppressed because it is too large
+ 39 - 29
dist/inputmask.js


File diff suppressed because it is too large
+ 2 - 2
dist/inputmask.min.js


File diff suppressed because it is too large
+ 39 - 29
dist/jquery.inputmask.js


File diff suppressed because it is too large
+ 2 - 2
dist/jquery.inputmask.min.js


+ 4 - 2
lib/extensions/inputmask.date.extensions.js

@@ -241,7 +241,8 @@ Inputmask.extendAliases({
 			],
 			ordinalSuffix: ["st", "nd", "rd", "th"]
 		},
-		preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos) {
+		preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos, strict) {
+			if (strict) return true;
 			var calcPos = 0, targetMatch, match;
 			if (isNaN(c) && buffer[pos] !== c) {
 				getTokenizer(opts).lastIndex = 0;
@@ -266,7 +267,8 @@ Inputmask.extendAliases({
 			}
 			return true;
 		},
-		postValidation: function (buffer, pos, currentResult, opts, maskset) {
+		postValidation: function (buffer, pos, currentResult, opts, maskset, strict) {
+			if (strict) return true;
 			opts.min = analyseMask(opts.min, opts.inputFormat, opts);
 			opts.max = analyseMask(opts.max, opts.inputFormat, opts);
 

+ 2 - 2
lib/extensions/inputmask.extensions.js

@@ -21,7 +21,7 @@ Inputmask.extendDefinitions({
 	}
 });
 
-
+var ipValidatorRegex = new RegExp("25[0-5]|2[0-4][0-9]|[01][0-9][0-9]");
 function ipValidator(chrs, maskset, pos, strict, opts) {
 	if (pos - 1 > -1 && maskset.buffer[pos - 1] !== ".") {
 		chrs = maskset.buffer[pos - 1] + chrs;
@@ -29,7 +29,7 @@ function ipValidator(chrs, maskset, pos, strict, opts) {
 			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);
+	return ipValidatorRegex.test(chrs);
 }
 
 

+ 20 - 12
lib/extensions/inputmask.numeric.extensions.js

@@ -26,8 +26,8 @@ function autoEscape(txt, opts) {
 	return escapedTxt;
 }
 
-function alignDigits(buffer, digits, opts) {
-	if (digits > 0 && !opts.digitsOptional) {
+function alignDigits(buffer, digits, opts, force) {
+	if (digits > 0 && (!opts.digitsOptional || force)) {
 		var radixPosition = $.inArray(opts.radixPoint, buffer);
 		if (radixPosition === -1) {
 			buffer.push(opts.radixPoint);
@@ -193,13 +193,17 @@ function decimalValidator(chrs, maskset, pos, strict, opts) {
 
 function checkForLeadingZeroes(buffer, opts) {
 	//check leading zeros
-	var numberMatches = new RegExp("(^" + (opts.negationSymbol.front != "" ? Inputmask.escapeRegex(opts.negationSymbol.front) + "?" : "") + Inputmask.escapeRegex(opts.prefix) + ")(.*)(" + Inputmask.escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? Inputmask.escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(buffer.slice().reverse().join("")),
-		number = numberMatches ? numberMatches[2] : "", leadingzeroes = false;
-	if (number) {
-		number = number.split(opts.radixPoint.charAt(0))[0];
-		leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
+	try {
+		var numberMatches = new RegExp("(^" + (opts.negationSymbol.front !== "" ? Inputmask.escapeRegex(opts.negationSymbol.front) + "?" : "") + Inputmask.escapeRegex(opts.prefix) + ")(.*)(" + Inputmask.escapeRegex(opts.suffix) + (opts.negationSymbol.back != "" ? Inputmask.escapeRegex(opts.negationSymbol.back) + "?" : "") + "$)").exec(buffer.slice().reverse().join("")),
+			number = numberMatches ? numberMatches[2] : "", leadingzeroes = false;
+		if (number) {
+			number = number.split(opts.radixPoint.charAt(0))[0];
+			leadingzeroes = new RegExp("^[0" + opts.groupSeparator + "]*").exec(number);
+		}
+		return leadingzeroes && (leadingzeroes[0].length > 1 || leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length) ? leadingzeroes : false;
+	} catch(e){
+		console.log(buffer.slice().reverse().join(""));
 	}
-	return leadingzeroes && (leadingzeroes[0].length > 1 || leadingzeroes[0].length > 0 && leadingzeroes[0].length < number.length) ? leadingzeroes : false;
 }
 
 //number aliases
@@ -254,7 +258,7 @@ Inputmask.extendAliases({
 				}
 			}
 		},
-		preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos) {
+		preValidation: function (buffer, pos, c, isSelection, opts, maskset, caretPos, strict) {
 			if (opts.__financeInput !== false && c === opts.radixPoint) return false;
 			var radixPos = $.inArray(opts.radixPoint, buffer);
 			pos = hanndleRadixDance(pos, c, radixPos, opts);
@@ -276,6 +280,8 @@ Inputmask.extendAliases({
 					caret: radixPos > pos ? pos + 1 : pos
 				};
 			}
+
+			if (strict) return true;
 			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
@@ -284,7 +290,8 @@ Inputmask.extendAliases({
 
 			return {rewritePosition: (isSelection && opts.digitsOptional) ? caretPos.end : pos};
 		},
-		postValidation: function (buffer, pos, currentResult, opts, maskset) {
+		postValidation: function (buffer, pos, currentResult, opts, maskset, strict) {
+			if (strict) return true;
 			if (opts.min !== null || opts.max !== null) {
 				var unmasked = opts.onUnMask(buffer.slice().reverse().join(""), undefined, $.extend({}, opts, {
 					unmaskAsNumber: true
@@ -343,7 +350,8 @@ Inputmask.extendAliases({
 
 			var valueParts = initialValue.split(radixPoint),
 				integerPart = valueParts[0].replace(/[^\-0-9]/g, ""),
-				decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "";
+				decimalPart = valueParts.length > 1 ? valueParts[1].replace(/[^0-9]/g, "") : "",
+				forceDigits = valueParts.length > 1;
 
 			initialValue = integerPart + (decimalPart !== "" ? radixPoint + decimalPart : decimalPart);
 
@@ -379,7 +387,7 @@ Inputmask.extendAliases({
 				}
 			}
 
-			return alignDigits(initialValue.toString().split(""), digits, opts).join("");
+			return alignDigits(initialValue.toString().split(""), digits, opts, forceDigits).join("");
 		},
 		onBeforeWrite: function (e, buffer, caretPos, opts) {
 			function stripBuffer(buffer, stripRadix) {

+ 4 - 4
lib/inputmask.js

@@ -87,8 +87,8 @@ Inputmask.prototype = {
 		//specify keyCodes which should not be considered in the keypress event, otherwise the preventDefault will stop their default behavior especially in FF
 		ignorables: [8, 9, 13, 19, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 93, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 0, 229],
 		isComplete: null, //override for isComplete - args => buffer, opts - return true || false
-		preValidation: null, //hook to preValidate the input.  Usefull for validating regardless the definition.	args => buffer, pos, char, isSelection, opts, maskset, caretPos => return true/false/command object
-		postValidation: null, //hook to postValidate the result from isValid.	Usefull for validating the entry as a whole.	args => buffer, pos, currentResult, opts => return true/false/json
+		preValidation: null, //hook to preValidate the input.  Usefull for validating regardless the definition.	args => buffer, pos, char, isSelection, opts, maskset, caretPos, strict => return true/false/command object
+		postValidation: null, //hook to postValidate the result from isValid.	Usefull for validating the entry as a whole.	args => buffer, pos, currentResult, opts, maskset, strict => return true/false/json
 		staticDefinitionSymbol: undefined, //specify a definitionSymbol for static content, used to make matches for alternators
 		jitMasking: false, //just in time masking ~ only mask while typing, can n (number), true or false
 		nullable: true, //return nothing instead of the buffertemplate when the user hasn't entered anything.
@@ -338,9 +338,9 @@ Inputmask.setValue = function (elems, value) {
 		if (el.inputmask) el.inputmask.setValue(value); else $(el).trigger("setvalue", [value]);
 	});
 };
+var escapeRegexRegex = new RegExp("(\\" + ["/", ".", "*", "+", "?", "|", "(", ")", "[", "]", "{", "}", "\\", "$", "^"].join("|\\") + ")", "gim");
 Inputmask.escapeRegex = function (str) {
-	var specials = ["/", ".", "*", "+", "?", "|", "(", ")", "[", "]", "{", "}", "\\", "$", "^"];
-	return str.replace(new RegExp("(\\" + specials.join("|\\") + ")", "gim"), "\\$1");
+	return str.replace(escapeRegexRegex, "\\$1");
 };
 Inputmask.keyCode = {
 	BACKSPACE: 8,

+ 8 - 6
lib/maskScope.js

@@ -530,6 +530,7 @@ module.exports = function maskScope(actionObj, maskset, opts) {
 	}
 
 	function refreshFromBuffer(start, end, buffer) {
+		// checkVal.call(inputmask, el, false, true, isRTL ? buffer.reverse() : buffer);
 		var i, p, skipOptionalPartCharacter = opts.skipOptionalPartCharacter;
 		opts.skipOptionalPartCharacter = "";
 		if (start === true) {
@@ -544,7 +545,7 @@ module.exports = function maskScope(actionObj, maskset, opts) {
 		}
 		p = start;
 		for (i = start; i < end; i++) {
-			var valResult = isValid(p, buffer[i], opts.negationSymbol ? buffer[i] !== opts.negationSymbol.front : true, opts.negationSymbol ? buffer[i] !== opts.negationSymbol.front : true);  //hackery for + validator (numeric alias)
+			var valResult = isValid(p, buffer[i], true, true);
 			if (valResult !== false) {
 				p = (valResult.caret !== undefined && valResult.caret > valResult.pos) ? valResult.caret : valResult.pos + 1;
 			}
@@ -799,8 +800,8 @@ module.exports = function maskScope(actionObj, maskset, opts) {
 		var result = true,
 			positionsClone = $.extend(true, {}, maskset.validPositions); //clone the currentPositions
 
-		if ($.isFunction(opts.preValidation) && !strict && fromIsValid !== true && validateOnly !== true && fromAlternate !== true) {
-			result = opts.preValidation(getBuffer(), maskPos, c, isSelection(pos), opts, maskset, pos);
+		if ($.isFunction(opts.preValidation) && fromIsValid !== true && validateOnly !== true && fromAlternate !== true) {
+			result = opts.preValidation(getBuffer(), maskPos, c, isSelection(pos), opts, maskset, pos, strict);
 			result = processCommandObject(result);
 		}
 		if (result === true) { //preValidation result
@@ -853,8 +854,8 @@ module.exports = function maskScope(actionObj, maskset, opts) {
 				};
 			}
 		}
-		if ($.isFunction(opts.postValidation) && result !== false && !strict && fromIsValid !== true && validateOnly !== true) {
-			var postResult = opts.postValidation(getBuffer(true), pos.begin !== undefined ? (isRTL ? pos.end : pos.begin) : pos, result, opts, maskset);
+		if ($.isFunction(opts.postValidation) && result !== false && fromIsValid !== true && validateOnly !== true) {
+			var postResult = opts.postValidation(getBuffer(true), pos.begin !== undefined ? (isRTL ? pos.end : pos.begin) : pos, result, opts, maskset, strict);
 			if (postResult !== undefined) {
 				result = postResult === true ? result : postResult;
 			}
@@ -1752,6 +1753,7 @@ module.exports = function maskScope(actionObj, maskset, opts) {
 		}
 
 		resetMaskSet();
+		maskset.tests = {}; //reset tests ~ possible after alternating
 		initialNdx = opts.radixPoint ? determineNewCaretPosition(0) : 0;
 		maskset.p = initialNdx;
 		inputmask.caretPos = {begin: initialNdx};
@@ -1764,7 +1766,7 @@ module.exports = function maskScope(actionObj, maskset, opts) {
 					maskset.p++;
 				} else {
 					var keypress = new $.Event("_checkval");
-					keypress.which = charCode.charCodeAt(0);
+					keypress.which = charCode.toString().charCodeAt(0);
 					charCodes += charCode;
 					var lvp = getLastValidPosition(undefined, true);
 					if (!isTemplateMatch(initialNdx, charCodes)) {

+ 2 - 2
package.json

@@ -1,6 +1,6 @@
 {
   "name": "inputmask",
-  "version": "5.0.0-beta.262",
+  "version": "5.0.0-beta.263",
   "description": "Inputmask is a javascript library which creates an input mask.  Inputmask can run against vanilla javascript, jQuery and jqlite.",
   "main": "index.js",
   "files": [
@@ -10,7 +10,7 @@
   ],
   "scripts": {
     "start": "webpack --progress --watch --config-name main --config webpack.config.js",
-    "start_jquery": "webpack --progress -watch --config-name jquery --config webpack.jqueryconfig.js",
+    "jquery": "webpack --progress --watch --config-name jquery --config webpack.config.js",
     "test": "grunt validate"
   },
   "repository": {

+ 4 - 3
qunit/tests_numeric.js

@@ -1678,7 +1678,7 @@ export default function (qunit, Inputmask) {
 		Inputmask("decimal", {
 			radixPoint: ",",
 			digits: "2",
-			autoUnmask: true,
+			autoUnmask: false,
 			suffix: " €"
 		}).mask(testmask);
 		testmask.focus();
@@ -1741,7 +1741,7 @@ export default function (qunit, Inputmask) {
 		$.caret(testmask, 3);
 		$("#testmask").SendKey(Inputmask.keyCode.BACKSPACE);
 
-		assert.equal(testmask.value, "$ 0", "Result " + testmask.value);
+		assert.equal(testmask.inputmask._valueGet(true), "$ 0", "Result " + testmask.inputmask._valueGet(true));
 	});
 
 	qunit.test("numeric + groupSeparator: \"  \" delete, - krajcot", function (assert) {
@@ -1757,7 +1757,7 @@ export default function (qunit, Inputmask) {
 		$.caret(testmask, 2);
 		$("#testmask").SendKey(Inputmask.keyCode.DELETE);
 
-		assert.equal(testmask.value, "$ 0", "Result " + testmask.value);
+		assert.equal(testmask.inputmask._valueGet(true), "$ 0", "Result " + testmask.inputmask._valueGet(true));
 	});
 
 	qunit.test("minvalue, - serGlazkov", function (assert) {
@@ -1923,6 +1923,7 @@ export default function (qunit, Inputmask) {
 		$("#testmask").SendKey(Inputmask.keyCode.DELETE);
 		$("#testmask").SendKey(Inputmask.keyCode.DELETE);
 		$("#testmask").SendKey(Inputmask.keyCode.DELETE);
+		$("#testmask").SendKey(Inputmask.keyCode.DELETE);
 
 		assert.equal(testmask.value, "0.00", "Result " + testmask.value);
 	});