PM-Riabel PM-Riabel - 3 months ago 10
HTML Question

HTML RegEx multiple matching not working

I need to validate two types of phone number formats with in one input.
The patterns are,

+XX XXX XX XX XX or

+XX X XXX XX XX

so I use the following pattern

<input type="tel" name="phone" id="phone" class="input" pattern="^\+\d{2} \d{1,3} \d{2,3} \d{2} \d{2}$" required />


but it validates some other patterns like +xx xx xx xx xx

I need to know how to use HTML RegEx exactly for the above mentioned patterns.

Thanks in advance

Answer

Use | for or operation

This will work

^\+\d{2} (\d{1} \d{3}|\d{3} \d{2}) \d{2} \d{2}$
Comments