首页 文章

离子2:形式验证器(正则表达式)

提问于
浏览
0

我尝试在我的表单上使用验证器来获取电话号码 . 我这样试试:

<input *ngIf="fieldForm.value.type == 'phone'" pattern="^([0-9]{2}\s?){5}$" type="text" class="form-control" formControlName="field" [(ngModel)]="value">

我希望用这种模式在两个数字之间接受空格或不接受:

^([0-9]{2}\s?){5}$

Example :

1- 01 01 01 01 01
2- 0101010101
3- 01 0101 0101

可以接受 .

Problem : 我的模式仅在我的示例中以第二种形式接受 . (0101010101)我不明白,因为我在http://regexr.com/中尝试了我的模式,它适用于我所有的例子 .

一个主意 ?

PS :我想验证一个法国电话号码 .

1 回答

  • 2

    正则表达式是一个邪恶的野兽 . Luckily I found this nice little example on regexlib.com

    这似乎是你正在寻找的那种 . 至于为什么Angular不匹配你的空格,可能你必须双重转义你的空格选择器,所以它看起来像:

    ^([0-9]{2}\\s?){5}$
    

    但这只是猜测 .

相关问题