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

时间:2018-08-29 10:59:02

标签: 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

此处是模板代码中的验证消息!

 <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>

逻辑:    为了在此处传递Regex字符串,请在此处使用Validators.Pattern()方法。请在此处建议使用模式,以便在此处输入数值

 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*)?$/")];
            }
          }

请在此处建议一个正则表达式模式Validators.pattern(“ / ^-?(0 | [1-9] \ d *)?$ /”)

0 个答案:

没有答案