【问题标题】:Angular Validation: Input should either contain Latin or Georgian characters, but not both of them together角度验证:输入应包含拉丁字符或格鲁吉亚字符,但不能同时包含两者
【发布时间】:2021-09-26 16:06:06
【问题描述】:

这是我这部分的模板,但它似乎不起作用。

(('^[\u10D0-\u10F0]+$')||('^[a-zA-Z]+$'))&&!(('^[\u10D0-\u10F0]+$')&&('^[a-zA-Z]+$'))

此外,它不应包含任何其他字符或数字,只能包含格鲁吉亚语或拉丁语。

【问题讨论】:

  • 请提供完整的相关代码行。我猜你使用的是Validators.pattern 对吧?
  • 是的,我使用这些。 firstName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(50), Validators.pattern('(('^[\u10D0-\u10F0]+$')||('^ [a-zA-Z]+$'))&&!(('^[\u10D0-\u10F0]+$')&&('^[a-zA-Z]+$'))')]],显然,这个表达式是不正确的。

标签: angular regex validation


【解决方案1】:

你可以使用

Validators.pattern(/^([a-zA-Z]+|[\u10D0-\u10F0]+)$/)
Validators.pattern('([a-zA-Z]+|[\\u10D0-\\u10F0]+)')

在第二行代码中,由于正则表达式是用字符串文字设置的,因此 Angular 会自动添加 ^$

详情

  • ^ - 字符串开头
  • ([a-zA-Z]+|[\u10D0-\u10F0]+) - 组 1 匹配一个或多个 ASCII 字母或一个或多个与 \u10D0-\u10F0 范围匹配的字符(从 的格文字母)
  • $ - 字符串结束。

【讨论】:

  • 成功了!也感谢您的详细解释。
猜你喜欢
  • 2021-08-07
  • 2012-09-20
  • 2010-12-27
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多