Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 仅使用自定义验证程序2将输入字段限制为数字_Angular_Typescript_Ecmascript 6_Angular Forms - Fatal编程技术网

Angular 仅使用自定义验证程序2将输入字段限制为数字

Angular 仅使用自定义验证程序2将输入字段限制为数字,angular,typescript,ecmascript-6,angular-forms,Angular,Typescript,Ecmascript 6,Angular Forms,我做了很多研究来找到解决这个问题的方法,但没有一个斯塔科弗流的问题有一个公认的答案。所以我做了研究,却找不到答案 我想让自定义验证器做的是,它应该只接受数字作为输入,并且只接受三位数的数字,如838、892183234。。诸如此类 现在它也接受字符 threeNumbers(control: FormControl) { if (control.value && !/[0-9\+\-\ ]/.test(control.value) && control

我做了很多研究来找到解决这个问题的方法,但没有一个斯塔科弗流的问题有一个公认的答案。所以我做了研究,却找不到答案

我想让自定义验证器做的是,它应该只接受数字作为输入,并且只接受三位数的数字,如838、892183234。。诸如此类

现在它也接受字符

  threeNumbers(control: FormControl) {
    if (control.value && !/[0-9\+\-\ ]/.test(control.value) && control.value.length < 3) {
      return { 'greater than 3 numbers': true };
    }
    return null;
  }

}

this.courseForm = this.fb.group({
  username: [null, [Validators.required, this.threeNumbers, Validators.maxLength(3)]],
  email: [null, [Validators.required, Validators.pattern('([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_.-]+)\\.([a-zA-Z]{2,5})')]],
  address: [null, [Validators.required, Validators.minLength(10), Validators.maxLength(100)]],
  select: [null, [Validators.required]]
});
三个数字(控件:FormControl){
if(control.value&!/[0-9\+\-\]/.test(control.value)&&control.value.length<3){
返回{“大于3个数字”:true};
}
返回null;
}
}
this.courseForm=this.fb.group({
用户名:[null,[Validators.required,this.threeNumbers,Validators.maxLength(3)],
电子邮件:[null,[Validators.required,Validators.pattern('([a-zA-Z0-9.-]+)@([a-zA-Z0-9.-]+)\\.([a-zA-Z]{2,5})],
地址:[null,[Validators.required,Validators.minLength(10),Validators.maxLength(100)],
选择:[null,[Validators.required]]
});
//html模板

  <form class="form-horizontal" (ngSubmit)='onSubmit()' [formGroup]='courseForm'>
    <div class="form-group">
      <label for="inputUsername" class="col-sm-2 control-label">Username</label>
      <div class="col-sm-10">
        <input type="text" class="form-control" id="inputUsername" placeholder="Username" formControlName="username" name="name"
          [ngClass]="{inValid: !courseForm.get('username').valid && courseForm.get('username').touched, valid: courseForm.get('username').valid && courseForm.get('username').touched}">
        <span class="help-block" *ngIf="!courseForm.get('username').valid && courseForm.get('username').touched">Please enter a valid username</span>
      </div>
    </div>

用户名
请输入有效的用户名
调用下面的函数打开(模糊)-

}
使输入type=“number”,它将停止使用字母表。

这可能只是改进了您使用的正则表达式。我也尝试了许多其他正则表达式,但它也接受字符,而不仅仅是数字。如果您可以建议一种模式,我将尝试它[0-9][0-9][0-9]您的问题与
/^\d+$/.test(control.value)
是只传递数字的正则表达式。所以,如果有一些值(
control.value
),它只有数字(
/^\d+$/.test(control.value)
),并且值只包含3个符号(
control.value.length==3
),那么我告诉angular它是有效值(
返回null
),否则我返回object
{'error':true}
告诉angular该值无效请在回答之前仔细阅读问题。我想通过定制验证器来实现,任何没有内置的东西,我们称之为定制。在这里,我要求你们调用上面的模糊方法。大概是这样的:-
validator(val){
if(val.length === 3){
    return true;
}else{
    return false;
}