@ -60,8 +60,21 @@ class Requirement {
validate = ( count : number ) = > { this . valid = ( count >= this . _minCount ) ; }
validate = ( count : number ) = > { this . valid = ( count >= this . _minCount ) ; }
}
}
export function initValidator ( passwordField : HTMLInputElement , rePasswordField : HTMLInputElement , submitButton : HTMLInputElement , submitSpan : HTMLSpanElement , validatorFunc ? : ( oncomplete : ( valid : boolean ) = > void ) = > void ) : ( { [ category : string ] : Requirement } | ( ( ) = > void ) ) [ ] {
export interface ValidatorConf {
var defaultPwValStrings : pwValStrings = {
passwordField : HTMLInputElement ;
rePasswordField : HTMLInputElement ;
submitInput? : HTMLInputElement ;
submitButton : HTMLSpanElement ;
validatorFunc ? : ( oncomplete : ( valid : boolean ) = > void ) = > void ;
}
export interface Validation { [ name : string ] : number }
export interface Requirements { [ category : string ] : Requirement } ;
export class Validator {
private _conf : ValidatorConf ;
private _requirements : Requirements = { } ;
private _defaultPwValStrings : pwValStrings = {
length : {
length : {
singular : "Must have at least {n} character" ,
singular : "Must have at least {n} character" ,
plural : "Must have at least {n} characters"
plural : "Must have at least {n} characters"
@ -82,39 +95,34 @@ export function initValidator(passwordField: HTMLInputElement, rePasswordField:
singular : "Must have at least {n} special character" ,
singular : "Must have at least {n} special character" ,
plural : "Must have at least {n} special characters"
plural : "Must have at least {n} special characters"
}
}
}
} ;
const checkPasswords = ( ) = > {
private _ checkPasswords = ( ) = > {
return passwordField . value == rePasswordField . value ;
return this . _conf . passwordField . value == this . _conf . rePasswordField . value ;
}
}
const checkValidity = ( ) = > {
validate = ( ) = > {
const pw = checkPasswords( ) ;
const pw = this . _ checkPasswords( ) ;
validatorFunc ( ( valid : boolean ) = > {
this . _conf . validatorFunc ( ( valid : boolean ) = > {
if ( pw && valid ) {
if ( pw && valid ) {
rePasswordField . setCustomValidity ( "" ) ;
this . _conf . rePasswordField . setCustomValidity ( "" ) ;
submitButton . disabled = false ;
if ( this . _conf . submitInput ) this . _conf . submitInput . disabled = false ;
submitSpa n. removeAttribute ( "disabled" ) ;
this . _conf . submitButto n. removeAttribute ( "disabled" ) ;
} else if ( ! pw ) {
} else if ( ! pw ) {
rePasswordField . setCustomValidity ( window . invalidPassword ) ;
this . _conf . rePasswordField . setCustomValidity ( window . invalidPassword ) ;
submitButton . disabled = true ;
if ( this . _conf . submitInput ) this . _conf . submitInput . disabled = true ;
submitSpa n. setAttribute ( "disabled" , "" ) ;
this . _conf . submitButto n. setAttribute ( "disabled" , "" ) ;
} else {
} else {
rePasswordField . setCustomValidity ( "" ) ;
this . _conf . rePasswordField . setCustomValidity ( "" ) ;
submitButton . disabled = true ;
if ( this . _conf . submitInput ) this . _conf . submitInput . disabled = true ;
submitSpa n. setAttribute ( "disabled" , "" ) ;
this . _conf . submitButto n. setAttribute ( "disabled" , "" ) ;
}
}
} ) ;
} ) ;
} ;
} ;
rePasswordField . addEventListener ( "keyup" , checkValidity ) ;
private _isInt = ( s : string ) : boolean = > { return ( s >= '0' && s <= '9' ) ; }
passwordField . addEventListener ( "keyup" , checkValidity ) ;
// Incredible code right here
private _testStrings = ( f : pwValString ) : boolean = > {
const isInt = ( s : string ) : boolean = > { return ( s in [ "0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" ] ) ; }
const testStrings = ( f : pwValString ) : boolean = > {
const testString = ( s : string ) : boolean = > {
const testString = ( s : string ) : boolean = > {
if ( s == "" || ! s . includes ( "{n}" ) ) { return false ; }
if ( s == "" || ! s . includes ( "{n}" ) ) { return false ; }
return true ;
return true ;
@ -122,14 +130,12 @@ export function initValidator(passwordField: HTMLInputElement, rePasswordField:
return testString ( f . singular ) && testString ( f . plural ) ;
return testString ( f . singular ) && testString ( f . plural ) ;
}
}
interface Validation { [ name : string ] : number }
private _validate = ( s : string ) : Validation = > {
const validate = ( s : string ) : Validation = > {
let v : Validation = { } ;
let v : Validation = { } ;
for ( let criteria of [ "length" , "lowercase" , "uppercase" , "number" , "special" ] ) { v [ criteria ] = 0 ; }
for ( let criteria of [ "length" , "lowercase" , "uppercase" , "number" , "special" ] ) { v [ criteria ] = 0 ; }
v [ "length" ] = s . length ;
v [ "length" ] = s . length ;
for ( let c of s ) {
for ( let c of s ) {
if ( isInt( c ) ) { v [ "number" ] ++ ; }
if ( this . _ isInt( c ) ) { v [ "number" ] ++ ; }
else {
else {
const upper = c . toUpperCase ( ) ;
const upper = c . toUpperCase ( ) ;
if ( upper == c . toLowerCase ( ) ) { v [ "special" ] ++ ; }
if ( upper == c . toLowerCase ( ) ) { v [ "special" ] ++ ; }
@ -141,27 +147,37 @@ export function initValidator(passwordField: HTMLInputElement, rePasswordField:
}
}
return v
return v
}
}
passwordField . addEventListener ( "keyup" , ( ) = > {
const v = validate ( passwordField . value ) ;
for ( let criteria in requirements ) {
requirements [ criteria ] . validate ( v [ criteria ] ) ;
}
} ) ;
var requirements : { [ category : string ] : Requirement } = { } ;
if ( ! window . validationStrings ) {
private _bindRequirements = ( ) = > {
window . validationStrings = defaultPwValStrings ;
} else {
for ( let category in window . validationStrings ) {
for ( let category in window . validationStrings ) {
if ( ! testStrings( window . validationStrings [ category ] ) ) {
if ( ! this . _testStrings ( window . validationStrings [ category ] ) ) {
window . validationStrings [ category ] = defaultPwValStrings[ category ] ;
window . validationStrings [ category ] = this . _defaultPwValStrings [ category ] ;
}
}
const el = document . getElementById ( "requirement-" + category ) ;
const el = document . getElementById ( "requirement-" + category ) ;
if ( el ) {
if ( typeof ( el ) === 'undefined' || el == null ) continue ;
requirements [ category ] = new Requirement ( category , el as HTMLLIElement ) ;
this . _requirements [ category ] = new Requirement ( category , el as HTMLLIElement ) ;
}
} ;
get requirements ( ) : Requirements { return this . _requirements } ;
constructor ( conf : ValidatorConf ) {
this . _conf = conf ;
if ( ! ( this . _conf . validatorFunc ) ) {
this . _conf . validatorFunc = ( oncomplete : ( valid : boolean ) = > void ) = > { oncomplete ( true ) ; } ;
}
this . _conf . rePasswordField . addEventListener ( "keyup" , this . validate ) ;
this . _conf . passwordField . addEventListener ( "keyup" , this . validate ) ;
this . _conf . passwordField . addEventListener ( "keyup" , ( ) = > {
const v = this . _validate ( this . _conf . passwordField . value ) ;
for ( let criteria in this . _requirements ) {
this . _requirements [ criteria ] . validate ( v [ criteria ] ) ;
}
}
} ) ;
if ( ! window . validationStrings ) {
window . validationStrings = this . _defaultPwValStrings ;
} else {
this . _bindRequirements ( ) ;
}
}
}
}
return [ requirements , checkValidity ]
}
}