简体   繁体   English

如何解决“'number' 类型的参数不能分配给'string | RegExp' 类型的参数。”

[英]How to fix "Argument of type 'number' is not assignable to parameter of type 'string | RegExp'."

I created a form in Angular and im trying to prevent the user from typing 'e' as a number.我在 Angular 中创建了一个表单,我试图阻止用户输入“e”作为数字。 I know i have to convert the user input into a string first, and then check for 'e', then turn it back to a number.我知道我必须先将用户输入转换为字符串,然后检查“e”,然后将其转回数字。

(I was also wondering how I can replace that 'e' into an empty string so it doesn't show up on the input text) (我还想知道如何将那个 'e' 替换为一个空字符串,这样它就不会出现在输入文本中)

this.signupForm = new FormGroup({
      'accountType': new FormControl('Personal', Validators.required),
      'name': new FormControl(null),
      'email': new FormControl(null,[Validators.required, Validators.email]),
      'city': new FormControl(null),
      'country': new FormControl(null, Validators.required),
      'zipCode': new FormControl(null, [Validators.pattern("[0-9]*"), Validators.pattern(e*), Validators.maxLength(5)]), 
      'dia': new FormControl(null)

    });

The validators only impact on the validity of the form, as follows:验证器只影响表单的有效性,如下所示:

// True if it complies with the validators, False if not
this.signupForm.valid

If what you want to do is restrict the input, you should do something like this:如果你想做的是限制输入,你应该这样做:

In the component.ts:在 component.ts 中:

public inputValidator(event: any) {
    this.signupForm.controls['zipCode'].setValue(event.target.value.replace(/[^0-9]*/g, "")); 
  }

Then in the html:然后在html中:

<input formControlName="zipCode" (input)="inputValidator($event)" type="text" maxlength="5" >

the "e" validator is not necessary because the regular ([Validators.pattern("[0-9]*")) expression already filters it “e”验证器不是必需的,因为正则 ([Validators.pattern("[0-9]*")) 表达式已经过滤了它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“数字”类型的参数不可分配给“字符串”类型的参数 | 正则表达式'' - How to fix ' Argument of type 'number' is not assignable to parameter of type 'string | RegExp' ' “RegExp”类型的参数不能分配给“string”类型的参数 - Argument of type 'RegExp' is not assignable to parameter of type 'string' “数字”类型的参数不能分配给“字符串”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' “字符串”类型的参数不能分配给“数字”类型的参数 - Argument of type 'string' is not assignable to parameter of type 'number' &#39;string | 类型的参数 number&#39; 不能分配给类型为 &#39;string&#39; 的参数。 “数字”类型不能分配给“字符串”类型 - Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string' 类型&#39;string |的参数 编号 string []&#39;不可分配给&#39;string&#39;类型的参数 - Argument of type 'string | number | string[]' is not assignable to parameter of type 'string' 参数类型编号不可分配给参数类型字符串 | 未定义类型编号不可分配给类型字符串 - Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string 参数类型Number | Function不能赋予参数类型Number - Argument type Number is not assignable to parameter type String|Function 'number' 类型的参数不能使用 ParseInt 分配给'string' 类型的参数 - Argument of type 'number' is not assignable to parameter of type 'string' using ParseInt “数字”类型的参数不能分配给“日期”类型的参数 - Argument of type 'number' is not assignable to parameter of type 'Date'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM