用于验证电话号码的角度表单验证

San*_*isy 3 javascript angular-validation angular angular7 angular8

我正在尝试使用 angular 中的正则表达式来验证电话号码

HTML 内容

<div class="form-group row">
                <input type="text" class="form-control" appPhoneMask placeholder="Mobile Number" autocomplete="off"
                    [ngClass]="{ 'is-invalid': (f.inputCountryCode.errors && mobileNumberform.submitted) }"
                    formControlName="inputCountryCode">
                <div *ngIf="(f.inputCountryCode.invalid ) || (f.inputCountryCode.invalid && (f.inputCountryCode.dirty || f.inputCountryCode.touched))"
                    class="invalid-feedback">
                    <div *ngIf="f.inputCountryCode.errors.required">This field is required.</div>
                    <div *ngIf="f.inputCountryCode.errors.pattern">Invalid phone number.</div>
                </div>
            </div>
Run Code Online (Sandbox Code Playgroud)

TS代码

 this.$form = this.$builder.group({
      selectCountryCode: [null, Validators.required],
      inputCountryCode: [null, [Validators.required, Validators.pattern("[0-9 ]{12}")]]
    });
Run Code Online (Sandbox Code Playgroud)

验证模式应该允许带空格的数字,因为我使用的是电话号码屏蔽,它在 3 位数字后添加了空格。

在此处输入图片说明

该模式不起作用,不断让电话号码验证为假

Angular 4 手机号码验证

允许数字和空格的字段的正则表达式

屏蔽指令

export class PhoneMaskDirective {

  constructor(public ngControl: NgControl) { }

  @HostListener('ngModelChange', ['$event'])
  onModelChange(event) {
    this.onInputChange(event, false);
  }

  @HostListener('keydown.backspace', ['$event'])
  keydownBackspace(event) {
    this.onInputChange(event.target.value, true);
  }


  onInputChange(event, backspace) {
    let newVal = event.replace(/\D/g, '');
    if (backspace && newVal.length <= 6) {
      newVal = newVal.substring(0, newVal.length - 1);
    }
    if (newVal.length === 0) {
      newVal = '';
    } else if (newVal.length <= 3) {
      newVal = newVal.replace(/^(\d{0,3})/, '$1');
    } else if (newVal.length <= 6) {
      newVal = newVal.replace(/^(\d{0,3})(\d{0,3})/, '$1 $2');
    } else if (newVal.length <= 9) {
      newVal = newVal.replace(/^(\d{0,3})(\d{0,3})(\d{0,4})/, '$1 $2 $3');
    } else {
      newVal = newVal.substring(0, 10);
      newVal = newVal.replace(/^(\d{0,3})(\d{0,3})(\d{0,4})/, '$1 $2 $3');
    }
    this.ngControl.valueAccessor.writeValue(newVal);
  }
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*han 7

您可以允许:

  • 9999 9999
  • +61 2 9999 9999
  • (02) 9999 9999
Validators.pattern('[- +()0-9]+')
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!Validators.pattern('[- +()0-9]{6,}') 如果您希望至少有 6 个字符。 (3认同)

Ann*_*nna 6

您的正则表达式需要 12 个符号[0-9 ],而您输入的仅包含 11 个。

更新您的正则表达式inputCountryCode"[0-9 ]{11}"

 this.$form = this.$builder.group({
      selectCountryCode: [null, Validators.required],
      inputCountryCode: [null, [Validators.required, Validators.pattern("[0-9 ]{11}")]]
    });
Run Code Online (Sandbox Code Playgroud)

或者您可以在输入的电话号码后添加一个空格,因此它将是 12 个符号。

但我更喜欢使用更具体的正则表达式作为电话号码'[0-9]{3} [0-9]{3} [0-9]{3}',因为使用您的模式电话号码11 1111 111 111111有效号码