From 333a733211fe61e9299b2b2c730387793f99e213 Mon Sep 17 00:00:00 2001 From: shankarThiyagaraajan Date: Wed, 2 Aug 2017 18:10:57 +0530 Subject: [PATCH 1/2] - ES5 min&max&length validation [35%]. --- src/demo/index3.html | 5 +++ src/js/multi_formValidator.js | 78 ++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 19 deletions(-) diff --git a/src/demo/index3.html b/src/demo/index3.html index df32609..6b26651 100644 --- a/src/demo/index3.html +++ b/src/demo/index3.html @@ -86,6 +86,11 @@






+
+ + +
+






diff --git a/src/js/multi_formValidator.js b/src/js/multi_formValidator.js index 0f5478f..f95a1fc 100644 --- a/src/js/multi_formValidator.js +++ b/src/js/multi_formValidator.js @@ -187,7 +187,7 @@ function jsValidator() { // Apply filter for Email elements. if (activeElem.type == 'email') new jsFilter().email(activeElem); // Apply filter for Numeric elements. - // if (activeElem.min || activeElem.max || activeElem.minLength || activeElem.maxLength) jsFilter.limit(activeElem); + if (activeElem.min || activeElem.max || activeElem.getAttribute('data-maxlength') || activeElem.minLength || activeElem.maxLength) new jsFilter().limit(activeElem); // Apply filter File elements. if (activeElem.type == 'file') new jsFilter().file(activeElem); // Apply filter with string, alphaNumeric and pregMatch. @@ -290,37 +290,77 @@ function jsFilter() { */ this.limit = function (element) { var status = this.checkStatus(element); - if (true === status) element.addEventListener('input', this.isInLimit, false); + if (true === status) element.addEventListener('change', this.isInLimit, false); }; /* * Restrict element with it's limit. */ this.isInLimit = function (event) { + // Load the element value. var value = event.target.value; + // To check is this action is from "windows" action or not. if (true === helper.isWindowAction(event)) return true; + + // Getting target element. + var target = event.target; + + // Final value to load back. + var final_value = value; + // Getting object from element. var min = event.target.min; var max = event.target.max; - // Default values for Min and Max. - if (!min) min = 1; - if (!max) max = 31; - // Forming pattern for Restriction. - var regex = new RegExp('^[0-9]+$'); - // Validation with Code. - var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); - //jsLogger.out('Limit', regex.test(key) + ' | min |' + min + ' | max | ' + max); - //jsLogger.out('Regex', regex.test(key)); - // Return status of the Action. - if (false === regex.test(key) || parseInt(value) > max || parseInt(value) < min) { - event.preventDefault(); - } - var num = +this.value; //converts value to a Number - if (!this.value.length) return false; //allows empty field - this.value = isNaN(num) ? min : num > max ? max : num < min ? min : num; + // Get max-length attribute from element. + var max_length = event.target.getAttribute('data-maxlength') ? event.target.getAttribute('data-maxlength') : 0; + max_length = parseInt(max_length); + var num = value; + + // if "max_length" is "0", then its don't have limit variables. + if (0 === max_length) { + + // Default values for Min and Max. + if (!min) min = 1; + if (!max) max = 31; + + // Forming pattern for Restriction. + var regex = new RegExp('^[0-9]+$'); + + // Validation with Code. + var key = String.fromCharCode(!event.charCode ? event.which : event.charCode); + + // Return status of the Action. + if (false === regex.test(key) || parseInt(value) > max || parseInt(value) < min) { + event.preventDefault(); + } + + // Parse to INT. + num = parseInt(num, 10); + + // // converts value to a Number. + if (isNaN(num)) { + target.value = ""; + return; + } + + // Check value is greater than "max", then replace "max". + if (parseInt(num) > max) final_value = max; + + // Check value is greater than "min", then replace "min". + if (parseInt(num) < min) final_value = min; + + } else { + //TODO: Min length later. + // Validate the length of the string. + if ((num.length > max_length) && 0 < max_length) { + // If length is more, then cutoff the remaining letters. + final_value = num.substr(0, max_length); + } + } - event.target.value = event.target.value.substring(0, event.target.value.length - 1); + // Revert value back to an element. + this.value = final_value; }; /* * Only allow alpha([a-zA-Z]). From b132cd87d1b88d52062b73dd993eb00adba28e51 Mon Sep 17 00:00:00 2001 From: shankarThiyagaraajan Date: Wed, 2 Aug 2017 19:09:41 +0530 Subject: [PATCH 2/2] - min&max&length validation. --- src/demo/index3.html | 2 +- src/js/multi_formValidator.es6.js | 80 +++++++++++++++++++++++-------- src/js/multi_formValidator.js | 3 ++ 3 files changed, 65 insertions(+), 20 deletions(-) diff --git a/src/demo/index3.html b/src/demo/index3.html index 6b26651..1123857 100644 --- a/src/demo/index3.html +++ b/src/demo/index3.html @@ -94,7 +94,7 @@ - +