![](/img/trans.png)
[英]How to fix "Argument of type 'number' is not assignable to parameter of type 'string | RegExp'."
[英]How to fix ' Argument of type 'number' is not assignable to parameter of type 'string | RegExp' '
我试图阻止用户使用任何特殊符号以及没有字符的空格。 当我尝试将它放在我的 FormGroup Validator 上时,我收到一条错误消息,提示“'number' 类型的参数不可分配给'string | 类型的参数”。 正则表达式''
this.houseForm = this.fb.group({
address: [
null,
[Validators.required, Validators.pattern(^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*)],
]
});
Validators.pattern()
需要一个字符串或一个正则表达式。 你什么都不提供。
您提供了^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*
,但您需要的是:
/^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*/
或
'^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*'
对于正则表达式,需要用斜杠/
括起来,对于字符串,需要用引号'
括起来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.