Regex 角度5:正则表达式模式不应该允许字母数字/字母表,它应该只允许数字

Regex 角度5:正则表达式模式不应该允许字母数字/字母表,它应该只允许数字,regex,validation,angular5,Regex,Validation,Angular5,模板代码中的验证消息 the Validators.pattern("Regex") should take any number but it should not allow the user to type any alphabet/alphanumeric value, no need of decimals either, it should allow only numeric values {{control.Hint} 请输入范围限制内的有效值。 输入的值应大于允许的最小值。

模板代码中的验证消息

the Validators.pattern("Regex") should take any number but it should not allow the user to type any alphabet/alphanumeric value, no need of decimals either, it should allow only numeric values

{{control.Hint}
请输入范围限制内的有效值。
输入的值应大于允许的最小值。
输入的值应小于允许的最大值。
输入的值只能是数字,不能是字母数字
逻辑: 在此处使用Validators.Pattern()方法,以便在此处传递正则表达式字符串。请建议此处的模式以允许此处的数值

 <ng-container id="NumericUpDown" *ngSwitchCase="5">
          <input type="number" [formControlName]="control.Name" [name]="control.Name" [id]="control.Id" [min]="control.Min" [max]="control.Max" step="1" [value]="control.Value">
              <span class ="tooltip-hint" *ngIf="control.Hint != null">{{control.Hint}}</span>
          <div *ngIf="form.controls[control.Name].invalid && (form.controls[control.Name].dirty || form.controls[control.Name].touched)" class="alert alert-danger">
            <span *ngIf="form.controls[control.Name].errors.required">Please enter a valid value within the Range Limits.</span>
            <span *ngIf="form.controls[control.Name].errors.min">Entered value should be greater than Minimum allowed value.</span>
            <span *ngIf="form.controls[control.Name].errors.max">Entered value should be less than Maximum allowed value.</span>
            <span *ngIf="form.controls[control.Name].errors.pattern">Entered value should be only numbers, not in alphanumerics</span>
          </div>
        </ng-container>
private createFormControl(控件:任意):FormControl{
设formControl:formControl=null;
开关(控制。控制类型){
case ControlTypes.RadioButton:
案例控制类型.TextBox:
案例控制类型.下拉列表:
case ControlTypes.CheckBox:
formControl=新的formControl({value:control.value | |“”,已禁用:!control.Enabled});
打破
案例控制类型.NumericUpDown:
formControl=newFormControl(control.Value | |“”,this.configureValidators(control));
打破
}
返回表单控制;
}
专用配置验证器(控件:任意):数组{
常量验证器:数组=新数组();
control.Validators.forEach(validator=>{
this.getValidator(validator,control).forEach(element=>validators.push(element));
});
返回验证器;
}
私有getValidator(validator类型:validator类型,控件:any):数组{
交换机(验证类型){
案例验证程序类型。必需:
返回[Validators.required];
案例验证程序类型。范围:
返回[Validators.min(control.min)、Validators.max(control.max)];
案例验证程序类型。模式:
返回[Validators.pattern(“/^-?(0 |[1-9]\d*)?$/”);
}
}

请在这里建议一个正则表达式模式验证器。模式(“/^-?(0 |[1-9]\d*)?$/”

尝试
验证器。模式(“-?(0 |[1-9]\\d*)?”
如果您需要禁止
0000
类似的输入。您面临的确切问题是什么?你只需要一个正则表达式就可以了吗?在这种情况下,粘贴的所有其他代码都是无用的。请澄清。是的,我只需要正则表达式声明,建议要求我输入代码,所以我保留了代码。我不想拒绝0000s。。我只想让数字向上向下,而不是字母表/字母数字-我想要正则表达式模式hi@ziaulrehmanmoghal,你能回答这个问题吗?正则表达式模式发送到验证器。模式(正则表达式模式)在这里
 private createFormControl(control: any): FormControl {
            let formControl: FormControl = null;
            switch (control.ControlType) {
              case ControlTypes.RadioButton:
              case ControlTypes.TextBox:
              case ControlTypes.DropDown:
              case ControlTypes.CheckBox:
                formControl = new FormControl({ value: control.Value || '', disabled: !control.Enabled });
                break;
              case ControlTypes.NumericUpDown:
                formControl = new FormControl(control.Value || '', this.configureValidators(control));
                break;
            }
            return formControl;
          }

          private configureValidators(control: any): Array<ValidatorFn> {
            const validators: Array<ValidatorFn> = new Array<ValidatorFn>();
            control.Validators.forEach(validator => {
              this.getValidator(validator, control).forEach(element => validators.push(element));
            });
            return validators;
          }

          private getValidator(ValidatorType: ValidatorTypes, control: any): Array<ValidatorFn> {
            switch (ValidatorType) {
              case ValidatorTypes.Required:
                return [Validators.required];
              case ValidatorTypes.Range:
                return [Validators.min(control.Min), Validators.max(control.Max)];
                case ValidatorTypes.Pattern:
                return [Validators.pattern("/^-?(0|[1-9]\d*)?$/")];
            }
          }