Skip to content

Commit 333a733

Browse files
author
shankarThiyagaraajan
committed
- ES5 min&max&length validation [35%].
1 parent 76921ea commit 333a733

2 files changed

Lines changed: 64 additions & 19 deletions

File tree

src/demo/index3.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<input type="number" required min="2" max="10" minlength="1" maxlength="2" name="rangeTester">
8787
</div>
8888
<br><br><br><br><br><br><br>
89+
<div>
90+
<label>Length Chars :</label>
91+
<input type="text" required min="4" max="50" data-maxlength="10" name="rangeTester">
92+
</div>
93+
<br><br><br><br><br><br><br>
8994
<button type="submit" name="btn-save" class="btn btn-default">Submit</button>
9095
</form>
9196
</div>

src/js/multi_formValidator.js

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ function jsValidator() {
187187
// Apply filter for Email elements.
188188
if (activeElem.type == 'email') new jsFilter().email(activeElem);
189189
// Apply filter for Numeric elements.
190-
// if (activeElem.min || activeElem.max || activeElem.minLength || activeElem.maxLength) jsFilter.limit(activeElem);
190+
if (activeElem.min || activeElem.max || activeElem.getAttribute('data-maxlength') || activeElem.minLength || activeElem.maxLength) new jsFilter().limit(activeElem);
191191
// Apply filter File elements.
192192
if (activeElem.type == 'file') new jsFilter().file(activeElem);
193193
// Apply filter with string, alphaNumeric and pregMatch.
@@ -290,37 +290,77 @@ function jsFilter() {
290290
*/
291291
this.limit = function (element) {
292292
var status = this.checkStatus(element);
293-
if (true === status) element.addEventListener('input', this.isInLimit, false);
293+
if (true === status) element.addEventListener('change', this.isInLimit, false);
294294
};
295295
/*
296296
* Restrict element with it's limit.
297297
*/
298298
this.isInLimit = function (event) {
299+
// Load the element value.
299300
var value = event.target.value;
301+
300302
// To check is this action is from "windows" action or not.
301303
if (true === helper.isWindowAction(event)) return true;
304+
305+
// Getting target element.
306+
var target = event.target;
307+
308+
// Final value to load back.
309+
var final_value = value;
310+
302311
// Getting object from element.
303312
var min = event.target.min;
304313
var max = event.target.max;
305-
// Default values for Min and Max.
306-
if (!min) min = 1;
307-
if (!max) max = 31;
308-
// Forming pattern for Restriction.
309-
var regex = new RegExp('^[0-9]+$');
310-
// Validation with Code.
311-
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
312-
//jsLogger.out('Limit', regex.test(key) + ' | min |' + min + ' | max | ' + max);
313-
//jsLogger.out('Regex', regex.test(key));
314-
// Return status of the Action.
315-
if (false === regex.test(key) || parseInt(value) > max || parseInt(value) < min) {
316-
event.preventDefault();
317-
}
318314

319-
var num = +this.value; //converts value to a Number
320-
if (!this.value.length) return false; //allows empty field
321-
this.value = isNaN(num) ? min : num > max ? max : num < min ? min : num;
315+
// Get max-length attribute from element.
316+
var max_length = event.target.getAttribute('data-maxlength') ? event.target.getAttribute('data-maxlength') : 0;
317+
max_length = parseInt(max_length);
318+
var num = value;
319+
320+
// if "max_length" is "0", then its don't have limit variables.
321+
if (0 === max_length) {
322+
323+
// Default values for Min and Max.
324+
if (!min) min = 1;
325+
if (!max) max = 31;
326+
327+
// Forming pattern for Restriction.
328+
var regex = new RegExp('^[0-9]+$');
329+
330+
// Validation with Code.
331+
var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
332+
333+
// Return status of the Action.
334+
if (false === regex.test(key) || parseInt(value) > max || parseInt(value) < min) {
335+
event.preventDefault();
336+
}
337+
338+
// Parse to INT.
339+
num = parseInt(num, 10);
340+
341+
// // converts value to a Number.
342+
if (isNaN(num)) {
343+
target.value = "";
344+
return;
345+
}
346+
347+
// Check value is greater than "max", then replace "max".
348+
if (parseInt(num) > max) final_value = max;
349+
350+
// Check value is greater than "min", then replace "min".
351+
if (parseInt(num) < min) final_value = min;
352+
353+
} else {
354+
//TODO: Min length later.
355+
// Validate the length of the string.
356+
if ((num.length > max_length) && 0 < max_length) {
357+
// If length is more, then cutoff the remaining letters.
358+
final_value = num.substr(0, max_length);
359+
}
360+
}
322361

323-
event.target.value = event.target.value.substring(0, event.target.value.length - 1);
362+
// Revert value back to an element.
363+
this.value = final_value;
324364
};
325365
/*
326366
* Only allow alpha([a-zA-Z]).

0 commit comments

Comments
 (0)