是否可以修改模式验证器以包括删除输入字段中写入的空格?

问题描述 投票:0回答:1

我正在运行一个验证器,在该验证器中,我检查输入的电话格式是否与后端请求的信息兼容。是否可以修改此正则表达式,以便在输入的数字之间写空格时删除或不考虑该正则表达式?这是我的验证器

    this.formGroup = this.fb.group({

      mobilePhone: [
        '',
        [
          Validators.required,
          CustomValidators.patternValidator(/^((\+)33|0|0033)[1-9](\d{2}){4}$/, { onlyNumber: true })
        ]
      ],
...


提前谢谢您?

regex angular requiredfieldvalidator customvalidator
1个回答
0
投票

如果Regex不起作用,那么您可以像这样创建用于手机号码验证的服务:

SERVICE文件(validaion.service.ts)

import * as PhoneNumber from 'awesome-phonenumber';

 static mobileNumberValidator(control) {
    const mobile = new PhoneNumber.default(control.value, 'US');
    return (!mobile.isValid() || !mobile.isMobile()) ? { invalidMobile: false } : null;
  }

TS文件

mobileNumber: ['', [Validators.required, 
Validators.maxLength(13), ValidationService.mobileNumberValidator]],

HTML

<p class="error" *ngIf="editProfileForm.get('mobileNumber').touched && yourformName['mobileNumber'].errors ">
<span class="error-message" 
*ngIf="!editProfile['mobileNumber'].errors.required && !editProfile.mobileNumber.errors.invalidMobile">Cell phone number is not valid</span>
 </p>
© www.soinside.com 2019 - 2024. All rights reserved.