@@ -196,7 +196,7 @@ class jsValidator {
196196 // Apply filter for Email elements.
197197 if ( activeElem . type == 'email' ) new jsFilter ( ) . email ( activeElem ) ;
198198 // Apply filter for Numeric elements.
199- // if (activeElem.min || activeElem.max || activeElem.minLength || activeElem.maxLength) jsFilter.limit(activeElem);
199+ if ( activeElem . min || activeElem . max || activeElem . getAttribute ( 'data-maxlength' ) || activeElem . minLength || activeElem . maxLength ) new jsFilter ( ) . limit ( activeElem ) ;
200200 // Apply filter File elements.
201201 if ( activeElem . type == 'file' ) new jsFilter ( ) . file ( activeElem ) ;
202202 // Apply filter with string, alphaNumeric and pregMatch.
@@ -307,39 +307,81 @@ class jsFilter {
307307 * Numeric with Limited elements filter listener.
308308 */
309309 limit ( element ) {
310+ let current = this ;
310311 let status = this . checkStatus ( element ) ;
311- if ( true === status ) element . addEventListener ( 'input ' , this . isInLimit , false ) ;
312+ if ( true === status ) element . addEventListener ( 'change ' , current . isInLimit , false ) ;
312313 } ;
313314
314315 /*
315316 * Restrict element with it's limit.
316317 */
317318 isInLimit ( event ) {
319+
320+ // Load the element value.
318321 let value = event . target . value ;
322+
319323 // To check is this action is from "windows" action or not.
320324 if ( true === helper . isWindowAction ( event ) ) return true ;
325+
326+ // Getting target element.
327+ let target = event . target ;
328+
329+ // Final value to load back.
330+ let final_value = value ;
331+
321332 // Getting object from element.
322333 let min = event . target . min ;
323334 let max = event . target . max ;
324- // Default values for Min and Max.
325- if ( ! min ) min = 1 ;
326- if ( ! max ) max = 31 ;
327- // Forming pattern for Restriction.
328- let regex = new RegExp ( '^[0-9]+$' ) ;
329- // Validation with Code.
330- let key = String . fromCharCode ( ! event . charCode ? event . which : event . charCode ) ;
331- //jsLogger.out('Limit', regex.test(key) + ' | min |' + min + ' | max | ' + max);
332- //jsLogger.out('Regex', regex.test(key));
333- // Return status of the Action.
334- if ( false === regex . test ( key ) || parseInt ( value ) > max || parseInt ( value ) < min ) {
335- event . preventDefault ( ) ;
336- }
337335
338- let num = + this . value ; //converts value to a Number
339- if ( ! this . value . length ) return false ; //allows empty field
340- this . value = isNaN ( num ) ? min : num > max ? max : num < min ? min : num ;
336+ // Get max-length attribute from element.
337+ let max_length = event . target . getAttribute ( 'data-maxlength' ) ? event . target . getAttribute ( 'data-maxlength' ) : 0 ;
338+ max_length = parseInt ( max_length ) ;
339+ let num = value ;
340+
341+ // if "max_length" is "0", then its don't have limit letiables.
342+ if ( 0 === max_length ) {
343+
344+ // Default values for Min and Max.
345+ if ( ! min ) min = 1 ;
346+ if ( ! max ) max = 31 ;
347+
348+ // Forming pattern for Restriction.
349+ let regex = new RegExp ( '^[0-9]+$' ) ;
350+
351+ // Validation with Code.
352+ let key = String . fromCharCode ( ! event . charCode ? event . which : event . charCode ) ;
353+
354+ // Return status of the Action.
355+ if ( false === regex . test ( key ) || parseInt ( value ) > max || parseInt ( value ) < min ) {
356+ event . preventDefault ( ) ;
357+ }
358+
359+ // Parse to INT.
360+ num = parseInt ( num , 10 ) ;
361+
362+ // // converts value to a Number.
363+ if ( isNaN ( num ) ) {
364+ target . value = "" ;
365+ return ;
366+ }
367+
368+ // Check value is greater than "max", then replace "max".
369+ if ( parseInt ( num ) > max ) final_value = max ;
370+
371+ // Check value is greater than "min", then replace "min".
372+ if ( parseInt ( num ) < min ) final_value = min ;
373+
374+ } else {
375+ //TODO: Min length later.
376+ // Validate the length of the string.
377+ if ( ( num . length > max_length ) && 0 < max_length ) {
378+ // If length is more, then cutoff the remaining letters.
379+ final_value = num . substr ( 0 , max_length ) ;
380+ }
381+ }
341382
342- event . target . value = event . target . value . substring ( 0 , event . target . value . length - 1 ) ;
383+ // Revert value back to an element.
384+ this . value = final_value ;
343385 } ;
344386
345387 /*
0 commit comments