你应该考虑让禁止使用的名字完全大写或完全小写。然后做必要的比较;
比如:
forbiddenUsernames = ['chris', 'ashutosh'];
ngOnInit() {
this.signupForm = new FormGroup({
'username': new FormControl(null, [Validators.required, this.forbiddenNames.bind(this)])
});
}
// validator is just a function, its our own validator
forbiddenNames(control: FormControl): {
[s: string]: boolean
} {
if (control.value && this.forbiddenUsernames.indexOf(control.value.toLowerCase()) !== -1) { // "-1" - did not find a match
return {
'nameIsForbidden': true
};
}
}
如果您的禁止姓名列表来自数据库,您只需在列表上应用一个映射即可获得小写列表。
forbiddenNames = [
'John',
'JANE',
'jacob',
'SaM',
'JeReMy',
'sIdDhArTh'
];
forbiddenNames.map(name => name.toLowerCase());