-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-length.directive.ts
55 lines (48 loc) · 1.48 KB
/
validate-length.directive.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import {Directive, Input, OnChanges, SimpleChanges} from '@angular/core';
import {AbstractControl, NG_VALIDATORS, Validator, ValidatorFn, Validators} from '@angular/forms';
export function getStrLength(str: string): number {
if (!str) {
str = '';
}
const len = str.length ? str.length : 0;
let reLen = 0;
for (let i = 0; i < len; i++) {
if (str.charCodeAt(i) < 27 || str.charCodeAt(i) > 126) {
// Full width
reLen += 2;
} else {
// Half width
reLen++;
}
}
return reLen;
}
export function charLengthValidator(maxLength: number): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
const str = control.value;
const reLen = getStrLength(str);
return reLen > maxLength ? {'validateLength': {maxLength}} : null;
};
}
@Directive({
selector: '[appValidateLength]',
providers: [{provide: NG_VALIDATORS, useExisting: ValidateLengthDirective, multi: true}]
})
export class ValidateLengthDirective implements OnChanges, Validator {
@Input() appValidateLength: number;
private valFn = Validators.nullValidator;
constructor() {
}
ngOnChanges(changes: SimpleChanges): void {
const change = changes['appValidateLength'];
if (change) {
const val: number | number = change.currentValue;
this.valFn = charLengthValidator(val);
} else {
this.valFn = Validators.nullValidator;
}
}
validate(control: AbstractControl): { [key: string]: any } {
return this.valFn(control);
}
}