Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UAE Phone number regular expression [closed]

Tags:

regex

I am building a regular expression for UAE numbers. I managed to run few cases but some how its not validating all.

Here is what I came up with.

To Validate numbers starting with: 00971 or +971 below expression works fine.

/^((\+971|00971){1}(2|3|4|6|7|9|50|51|52|55|56){1}([0-9]{7}))$/

e.g. 00971551234567

Now to validate numbers starting without 00971 or +971, I combined below expression with above:

/^((050|051|052|055|056){1}([0-9]{7}))$/

e.g. 0551234567

The Problem

Somehow below expression is not working to validate phone numbers like 041234567

/^((02|03|04|06|07|09){1}([0-9]{7}))$/

I tried combining all of expression in one expression but it wasn't working, then I realized only above expression wasn't working.

         [Country][area][7 digit number]
Pattern: +971 55 1234567

Expression should accept numbers like:

  • 00971551234567
  • +971551234567
  • +97141234567
  • 0551234567
  • 041234567

How can I make my regex work?

like image 831
pAt Avatar asked Dec 07 '22 00:12

pAt


1 Answers

Seems like you want something like this,

^(?:\+971|00971|0)?(?:50|51|52|55|56|2|3|4|6|7|9)\d{7}$

DEMO

> /^(?:\+971|00971|0)(?:2|3|4|6|7|9|50|51|52|55|56)[0-9]{7}$/.test("041234567")
true
> /^(?:\+971|00971|0)(?:2|3|4|6|7|9|50|51|52|55|56)[0-9]{7}$/.test("00971551234567")
true
> /^(?:\+971|00971|0)(?:2|3|4|6|7|9|50|51|52|55|56)[0-9]{7}$/.test("009715512345671")
false

> "041234567".match(/^(?:\+971|00971|0)?(?:50|51|52|55|56|2|3|4|6|7|9)\d{7}$/m); 
[ '041234567',
  index: 0,
  input: '041234567' ]
like image 199
Avinash Raj Avatar answered Dec 23 '22 19:12

Avinash Raj