Javascript 如何使用角度验证输入数字长度?

Javascript 如何使用角度验证输入数字长度?,javascript,html,angularjs,Javascript,Html,Angularjs,我想用angular验证一个9位数的数字 这是在我的模态中: <input type="number" class="form-control" ng-model="id" required> 谢谢你 您是否同时尝试了ngMinlength和ngMaxlength 例如: <input type="text" ng-minlength=9 ng-maxlength=9 /> 请查看与此相关的官方文件: 关于表单验证的非常好的教程:如Rahul Desai所述,您必

我想用angular验证一个9位数的数字

这是在我的模态中:

<input type="number" class="form-control" ng-model="id" required>

谢谢你

您是否同时尝试了
ngMinlength
ngMaxlength

例如:

<input type="text" ng-minlength=9 ng-maxlength=9 />

请查看与此相关的官方文件:


关于表单验证的非常好的教程:

如Rahul Desai所述,您必须使用ngMinlength和ngMaxlength。
但是既然你想用数字,你就必须把代码改成这样

<input type="number" ng-model="id" ng-minlength=9 ng-maxlength=9 required />
你不应该再需要最小/最大属性了

编辑:
我想第二个是我弄错了。应该是

ng-pattern="/^[0-9]{9}$/"


对于反应式表单,您应该以以下方式组合验证器:

formControlYourField: [
  "",
  Validators.compose([
    Validators.required,
    Validators.minLength(9),
    Validators.maxLength(9),
    Validators.pattern("^[0-9]*$"),
  ]),
]
当然,在你的.html

<input
  matInput
  required
  formControlName="formControlYourField"
  type="text"
  placeholder="123456789"
/>

我只是使用
模式作为验证器。以下代码将使字段
成为必需的
,并且必须是长度
10

ngOnInit() {
    this.firstForm = this.fb.group({
        firstCtrl: ['', [Validators.pattern('^[0-9]{10}$'), Validators.required]],
    });

你有min-max属性可以使用它,例如,最小值也可以是000000101。它对我不起作用。它接受(3…5)或(2.5.44)@Alaa'你到底是什么意思?您是同时尝试了这两种解决方案还是仅尝试了其中一种?我想我在第二次考试中犯了一个错误。那时候我没有测试过。这种模式肯定会奏效。我不知道角度是否允许“number”类型的点。
formControlYourField: [
  "",
  Validators.compose([
    Validators.required,
    Validators.minLength(9),
    Validators.maxLength(9),
    Validators.pattern("^[0-9]*$"),
  ]),
]
<input
  matInput
  required
  formControlName="formControlYourField"
  type="text"
  placeholder="123456789"
/>
ngOnInit() {
    this.firstForm = this.fb.group({
        firstCtrl: ['', [Validators.pattern('^[0-9]{10}$'), Validators.required]],
    });