Fix keyword validator

This commit is contained in:
Tobias Eidelpes 2021-05-03 17:01:38 +02:00
parent f5b8ef21a2
commit 653cc82b47

View File

@ -1,6 +1,19 @@
import {AbstractControl} from '@angular/forms'; import {AbstractControl} from '@angular/forms';
export function keywordsValidator(control: AbstractControl): { [key: string]: any } | null { export function keywordsValidator(control: AbstractControl): { [key: string]: any } | null {
const valid = /^((\w){3,},?){1,2}(\w{3,})?$/.test(control.value); if (control.value == undefined) {
return valid ? null : {'invalidKeywords': {value: control.value}}; return null;
}
let split: string[];
split = control.value.split(',');
if (split.length > 3) {
return {'invalidKeywords': {value: control.value}};
}
for (let i = 0; i < split.length; i++) {
const word = split[i].trim();
if (word.length < 3) {
return {'invalidKeywords': {value: control.value}};
}
}
return null;
} }