@@ -39,8 +39,6 @@ class jsValidator {
39
39
this . jsFormError = false ;
40
40
// Overall error list.
41
41
this . formErrorList = { } ;
42
- // Overall validation status.
43
- this . validationPass = false ;
44
42
// Common Logger Instance.
45
43
this . jsFilter = false ;
46
44
this . jsRuleSet = false ;
@@ -175,12 +173,8 @@ class jsValidator {
175
173
// Single step instance validator for Ajax form submissions.
176
174
validate ( ) {
177
175
// Initiate form Check.
178
- this . check ( ) ;
179
- // Return validation status.
180
- return this . validationPass ;
176
+ return this . check ( ) ;
181
177
}
182
-
183
- //};
184
178
}
185
179
186
180
/*
@@ -213,6 +207,10 @@ class jsFilter {
213
207
case 'string' :
214
208
element . addEventListener ( "keypress" , current . constructor . isAlphaNumeric , false ) ;
215
209
break ;
210
+ // Allow only alpha Numeric [a-zA-Z0-9] not special characters.
211
+ case 'password' :
212
+ element . addEventListener ( "keypress" , current . constructor . isValidPassword , false ) ;
213
+ break ;
216
214
// Allow based on the pattern given.
217
215
default :
218
216
element . addEventListener ( "keypress" , current . constructor . isPatternValid , false ) ;
@@ -238,7 +236,7 @@ class jsFilter {
238
236
static isInLimit ( event ) {
239
237
var value = event . target . value ;
240
238
// To check is this action is from "windows" action or not.
241
- if ( true === isWindowAction ( event ) ) return true ;
239
+ if ( true === helper . isWindowAction ( event ) ) return true ;
242
240
243
241
// Getting object from element.
244
242
var min = event . target . min ;
@@ -265,60 +263,52 @@ class jsFilter {
265
263
// Only allow alpha([a-zA-Z]).
266
264
static isAlpha ( event ) {
267
265
// To check is this action is from "windows" action or not.
268
- if ( true === isWindowAction ( event ) ) return true ;
269
- // Getting special characters list.
270
- var allow_special = event . target . getAttribute ( 'data-allowSpecial' ) ;
271
- // Set default values for special characters.
272
- if ( ! allow_special && allow_special == null ) allow_special = '' ;
273
- // Format to string.
274
- allow_special = allow_special . toString ( ) ;
275
- // Validate with special formed pattern.
276
- var regex = new RegExp ( '^[a-zA-Z' + allow_special + ']+$' ) ;
277
- // Validation with Code.
278
- var key = String . fromCharCode ( ! event . charCode ? event . which : event . charCode ) ;
279
- jsLogger . out ( 'Alpha' , regex . test ( key ) ) ;
266
+ if ( true === helper . isWindowAction ( event ) ) return true ;
267
+ var status = helper . patternValid ( event , 'a-zA-Z' ) ;
268
+ console . log ( status ) ;
280
269
// Return status of the Action.
281
- if ( false === regex . test ( key ) ) event . preventDefault ( ) ;
270
+ if ( false === status ) event . preventDefault ( ) ;
282
271
} ;
283
272
284
273
// Only allow alpha([a-zA-Z0-9]).
285
274
static isAlphaNumeric ( event ) {
286
275
// To check is this action is from "windows" action or not.
287
- if ( true === isWindowAction ( event ) ) return true ;
288
- // Getting special characters list.
289
- var allow_special = event . target . getAttribute ( 'data-allowSpecial' ) ;
290
- // Set default values for special characters.
291
- if ( ! allow_special && allow_special == null ) allow_special = '' ;
292
- // Format to string.
293
- allow_special = allow_special . toString ( ) ;
294
- // Validate with special formed pattern.
295
- var regex = new RegExp ( '^[a-zA-Z0-9' + allow_special + ']+$' ) ;
296
- // Validation with Code.
297
- var key = String . fromCharCode ( ! event . charCode ? event . which : event . charCode ) ;
298
- jsLogger . out ( 'Alpha' , regex . test ( key ) ) ;
276
+ if ( true === helper . isWindowAction ( event ) ) return true ;
277
+ // Managing the Pattern.
278
+ var status = helper . patternValid ( event , 'a-zA-Z0-9' ) ;
279
+ // Return status of the Action.
280
+ if ( false === status ) event . preventDefault ( ) ;
281
+ } ;
282
+
283
+ static isValidPassword ( event ) {
284
+ // Prevent using "space".
285
+ var charCode = ( event . which ) ? event . which : event . keyCode ;
286
+ if ( charCode === 32 ) {
287
+ event . preventDefault ( ) ;
288
+ return false ;
289
+ }
290
+ // To check is this action is from "windows" action or not.
291
+ if ( true === helper . isWindowAction ( event ) ) return true ;
292
+ // Managing the Pattern.
293
+ var status = helper . patternValid ( event , 'a-zA-Z0-9' ) ;
299
294
// Return status of the Action.
300
- if ( false === regex . test ( key ) ) event . preventDefault ( ) ;
295
+ if ( false === status ) event . preventDefault ( ) ;
301
296
} ;
302
297
303
298
// Only allow by pattern(ex. ^[a-zA-Z0-3@#$!_.]+$).
304
299
static isPatternValid ( event ) {
305
300
// To check is this action is from "windows" action or not.
306
- if ( true === isWindowAction ( event ) ) return true ;
307
- // Getting special characters list.
308
- var pattern = event . target . getAttribute ( 'data-allow' ) ;
309
- // Validate with special formed pattern.
310
- var regex = new RegExp ( pattern ) ;
311
- // Validation with Code.
312
- var key = String . fromCharCode ( ! event . charCode ? event . which : event . charCode ) ;
313
- jsLogger . out ( 'Alpha' , regex . test ( key ) ) ;
301
+ if ( true === helper . isWindowAction ( event ) ) return true ;
302
+ // Managing the Pattern.
303
+ var status = helper . patternValid ( event , 'a-zA-Z0-9' ) ;
314
304
// Return status of the Action.
315
- if ( false === regex . test ( key ) ) event . preventDefault ( ) ;
305
+ if ( false === status ) event . preventDefault ( ) ;
316
306
} ;
317
307
318
308
// Check is numeric or not.
319
309
static isNumberKey ( event ) {
320
310
// To check is this action is from "windows" action or not.
321
- if ( true === isWindowAction ( event ) ) return true ;
311
+ if ( true === helper . isWindowAction ( event ) ) return true ;
322
312
// Validation with Code.
323
313
var charCode = ( event . which ) ? event . which : event . keyCode ;
324
314
if ( charCode === 46 || charCode > 31 && ( charCode < 48 || charCode > 57 ) ) {
@@ -466,23 +456,29 @@ class jsRuleSets {
466
456
// To Check, whether the element have value or not.
467
457
static isSet ( elem ) {
468
458
var status = true ;
469
- // Check length and empty or not.
470
- if ( elem . length === 0 || elem . value === '' ) status = false ;
459
+ var value = elem . value ;
460
+ //TODO: Implement suitable solution for this.
461
+ if ( value . length == 0 || value == '' || value == ' ' ) status = false ;
471
462
return status ;
472
463
} ;
473
464
474
465
// To Check Element with Min Condition.
475
466
static min ( elem ) {
476
467
var status = true ;
468
+ var value = elem . value ;
469
+ var min = elem . min ;
477
470
//TODO: Implement suitable solution for this.
478
- // if (elem.length < elem. min && elem.length !== 0 ) status = false;
471
+ if ( value < min ) status = false ;
479
472
return status ;
480
473
} ;
481
474
482
475
// To Check Element with Max Condition.
483
476
static max ( elem ) {
484
477
var status = true ;
485
- if ( elem . value > elem . max ) status = false ;
478
+ var value = elem . value ;
479
+ var max = elem . max ;
480
+ //TODO: Implement suitable solution for this.
481
+ if ( value > max ) status = false ;
486
482
return status ;
487
483
} ;
488
484
@@ -514,11 +510,13 @@ class jsRuleSets {
514
510
var elem2_id = elem1 . getAttribute ( 'data-check' ) ;
515
511
516
512
if ( elem2_id == null ) elem2_id = elem1 . getAttribute ( 'data-parent' ) ;
517
-
518
513
elem2_id = elem2_id . toString ( ) ;
514
+
519
515
var elem2 = document . getElementById ( elem2_id ) ;
516
+
520
517
var status = true ;
521
- if ( ( elem1 . value !== elem2 . value ) && elem1 . length !== elem2 . length ) status = false ;
518
+ if ( elem1 . value !== elem2 . value ) status = false ;
519
+ jsLogger . out ( 'Compare Status' , status ) ;
522
520
return status ;
523
521
}
524
522
}
@@ -581,24 +579,56 @@ var jsLogger = {
581
579
}
582
580
} ;
583
581
584
- /*
585
- * To check the keyboard action is window action or not.
586
- */
587
- function isWindowAction ( event ) {
588
- // Getting the event to be triggered.
589
- var theEvent = event || window . event ;
590
- // Getting the type of event or code.
591
- var key = theEvent . keyCode || theEvent . which ;
592
-
593
- // Check with list of code and ignore holding.
594
- // Tab, Space, Home, End, Up, Down, Left, Right...
595
- if ( key === 9 || key === 32 || key === 13 || key === 8 || ( key >= 35 && key <= 40 ) ) { //TAB was pressed
596
- return true ;
582
+ var helper = {
583
+ /*
584
+ * To check the keyboard action is window action or not.
585
+ */
586
+ isWindowAction : function ( event ) {
587
+
588
+ // Getting the event to be triggered.
589
+ var theEvent = event || window . event ;
590
+ // Getting the type of event or code.
591
+ var key = theEvent . shiftKey || theEvent . which ;
592
+ // Check with list of code and ignore holding.
593
+ // Tab, Space, Home, End, Up, Down, Left, Right...
594
+ if ( key === 9 || key === 0 || key === 8 || key === 32 || key === 13 || key === 8 || ( key >= 35 && key <= 40 ) ) {
595
+ return true ;
596
+ }
597
+
598
+ // If not in list then check return with corresponding data.
599
+ key = String . fromCharCode ( key ) ;
600
+ // Return also if length is 0.
601
+ if ( key . length == 0 ) return true ;
602
+
603
+ // Finally return "false" for general keys.
604
+ return false ;
605
+ } ,
606
+ getDefaultPattern : function ( event , originalPattern ) {
607
+ if ( typeof originalPattern == 'undefined' ) var originalPattern = false ;
608
+ // Getting special characters list.
609
+ var allow_special = event . target . getAttribute ( 'data-allowSpecial' ) ;
610
+ var pattern = event . target . pattern ;
611
+ console . log ( pattern . length ) ;
612
+ var defaultPattern ;
613
+ // Set default values for special characters.
614
+ if ( ! allow_special && allow_special == null ) allow_special = '' ;
615
+ // Format to string.
616
+ allow_special = allow_special . toString ( ) ;
617
+
618
+ if ( pattern != '' && pattern . length > 0 && pattern != null ) {
619
+ defaultPattern = pattern ;
620
+ } else {
621
+ defaultPattern = '^[' + originalPattern + allow_special + ']+$' ;
622
+ }
623
+ return defaultPattern ;
624
+ } ,
625
+ patternValid : function ( event , pattern ) {
626
+ // Managing the Pattern.
627
+ var defaultPattern = this . getDefaultPattern ( event , pattern ) ;
628
+ // Validate with special formed pattern.
629
+ var regex = new RegExp ( defaultPattern ) ;
630
+ // Validation with Code.
631
+ var key = String . fromCharCode ( ! event . charCode ? event . which : event . charCode ) ;
632
+ return regex . test ( key ) ;
597
633
}
598
- // If not in list then check return with corresponding data.
599
- key = String . fromCharCode ( key ) ;
600
- // Return also if length is 0.
601
- if ( key . length == 0 ) return true ;
602
- // Finally return "false" for general keys.
603
- return false ;
604
- }
634
+ } ;
0 commit comments