Validating the regular expression number

I wrote this regex for a Lebanese phone number, basically it should start with

00961 or +961 , which is the international code , and then the area code , which

can be any digit from 0 to 9 or the cell code "70" or "76" or

"79" then 6 digit for sure

I encoded the following reg ex without the 6 digit part:

^(([0][0]|[+])([9][6][1])([0-9]{1}|[7][0]|[7][1]|[7][6]|[7][8]))$

when I want to add a code so that only 6 digits are allowed in the expression:

^(([0][0]|[+])([9][6][1])([0-9]{1}|[7][0]|[7][1]|[7][6]|[7][8])([0-9]{6}))$

It seems that it takes 5 or 6 digits, not 6 digits for sure

I find it hard to find something wrong

+4
source share
5 answers

use this regular expression ((00)|(\+))961((\d)|(7[0168]))\d{6}

+2
source

Here is what I will use.

 /^(00|\+)961(\d|7[069])\d{6}$/ 
  • 00 or +
  • 961
  • 1-digit number or 70 or 76 or 79
  • 6 digit number
+1
source

[0-9]{1} will also correspond to 7x cell codes, since 7 is between 0 and 9. This means that the β€œ5-digit cell number” will match 7 and six more digits.

+1
source

Try

  /^(00961|\+961)([0-9]|70|76|79)\d{6}$/.test( phonenumber ); //^ start of string // ^^^^^^^^^^^^^ 00961 or +0961 // ^^^^^^^^^^^^^^^^ a digit 0 to 9 or 70 or 76 or 79 // ^^^^^ 6 digits // ^ end of string 
0
source

Basement code forms a trap, as @ellak points out:

 /^((00)|(\+))961((\d)|(7[0168]))\d{6}$/.test("009617612345"); // true 

Here the code should look like this: 00 961 76 12345 ,

but RegEx practically breaks it down like this: 00 961 7 612345 , because 7 matches in \d , and the rest is combined exactly 6 digits and matched.

I'm not sure if this is really true, but I think this is not what you want, otherwise RegEx should work in your question.

Here is a long RegEx that avoids the trap:

 /^(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})$/ 

A few test results:

 /(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("009617012345") false /(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("009618012345") true /(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("009617612345") false /(00|\+)961([0-68-9]\d{6}|7[234579]\d{5}|7[0168]\d{6})/.test("0096176123456") true 
0
source

All Articles