如何验证Emoji的输入。如果输入Emoji需要显示错误信息。
表单域为
'name': new FormControl('', [Validators.required]),发布于 2021-11-19 09:33:28
创建自定义验证函数并在组件文件中使用它。
我创建了一个通用的自定义函数,并为此创建了一个组件文件:
custom-Validatiors.ts
export function Noemotics(): ValidatorFn {
return (control: AbstractControl): any => {
window.setTimeout(() => {
if (control.value && control.value != '') {
let trimedvalue = control.value.replace(/[^a-zA-Z0-9 ]/gm, '');
control.setValue(trimedvalue);
}
}, 10);
};
}现在,您可以在项目中的任何位置使用此函数。您必须将此函数导入到组件文件中。
import { Noemotics } from '../Shared/custom-validators';将此函数指定为您必须对表单控件进行验证的位置。在angular中,我们使用的就像下面给出的:
this.RegisterFormGroup = this.fb.group({
firstname: ['', Validators.compose([Validators.required, Noemotics()])]
});https://stackoverflow.com/questions/54740318
复制相似问题