Javascript “如何修复”;类型为'的参数;编号';不可分配给类型为';字符串| RegExp'&引用;
我在Angular中创建了一个表单,我试图阻止用户键入“e”作为数字。我知道我必须首先将用户输入转换为字符串,然后检查“e”,然后将其转换为数字 (我还想知道如何将“e”替换为空字符串,使其不会显示在输入文本中)Javascript “如何修复”;类型为'的参数;编号';不可分配给类型为';字符串| RegExp'&引用;,javascript,angular,typescript,Javascript,Angular,Typescript,我在Angular中创建了一个表单,我试图阻止用户键入“e”作为数字。我知道我必须首先将用户输入转换为字符串,然后检查“e”,然后将其转换为数字 (我还想知道如何将“e”替换为空字符串,使其不会显示在输入文本中) 验证器仅影响表单的有效性,如下所示: // True if it complies with the validators, False if not this.signupForm.valid 如果要限制输入,应执行以下操作: 在component.ts中: public inpu
验证器仅影响表单的有效性,如下所示:
// True if it complies with the validators, False if not
this.signupForm.valid
如果要限制输入,应执行以下操作:
在component.ts中:
public inputValidator(event: any) {
this.signupForm.controls['zipCode'].setValue(event.target.value.replace(/[^0-9]*/g, ""));
}
然后在html中:
<input formControlName="zipCode" (input)="inputValidator($event)" type="text" maxlength="5" >
“e”验证器是不必要的,因为正则([Validators.pattern([0-9]*”)表达式已经对其进行了过滤它应该是数字输入吗?如果是,我建议使用标准的HTML5元素。
是的,我这样做了,但u仍然可以添加“e”作为数字,e等于2.718
<input formControlName="zipCode" (input)="inputValidator($event)" type="text" maxlength="5" >