Input type number “only numeric value” validation

后端 未结 5 1712
耶瑟儿~
耶瑟儿~ 2021-02-03 17:43

How can I validate an input of type=\"number\" to only be valid if the value is numeric or null using only Reactive Forms (no directives)?
Only numbers

5条回答
  •  别跟我提以往
    2021-02-03 18:06

    In HTML file you can add ngIf for you pattern like this

    
    

    In .ts file you can add the Validators pattern - "^[0-9]*$"

    this.Mobile = new FormControl('', [
      Validators.required,
      Validators.pattern("^[0-9]*$"),
      Validators.minLength(8),
    ]);
    

提交回复
热议问题