V. Pivet V. Pivet - 1 year ago 177
Javascript Question

Ionic 2 : Form validator (regex)

I try to use validator on my form for a phone number. I try this way :

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


I want to accept with space or not between two number with this pattern :

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


Example :

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


Can be accepted.

Problem : My pattern accept only in the second form in my example. (0101010101)
I don't understand because I try my pattern in http://regexr.com/ and it work well for all my example.

An idea ?

PS : I want to verify a french phone number.

Answer Source

Regex is an evil beast. Luckily I found this nice little example on regexlib.com

This seems to be kind-of what you're looking for. As for why Angular isn't matching your whitespace, it may potentially be that you have to double-escape your whitespace selector, so it would look like:

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

But that's just a guess.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download