在 Angular 中进行此类验证可以使用自定义验证器。以下是一个示例代码,用于验证输入框中的内容是否同时包含拉丁字符和格鲁吉亚字符:
import { FormControl } from '@angular/forms';
export function noMixedChars(control: FormControl) {
const latinPattern = /^[\u0000-\u007F]+$/;
const georgianPattern = /^[\u10A0-\u10FF]+$/;
const value = control.value;
if (!value) {
return null;
}
if (latinPattern.test(value) && georgianPattern.test(value)) {
return { mixedChars: true };
}
return null;
}
在模板中使用时,可以这样写:
Input should either contain Latin or Georgian characters, but not both of them together
这样就会在输入框中出现错误信息,当同时输入拉丁字符和格鲁吉亚字符时,会将错误信息显示出来。