Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript regex to allow numbers and special characters but not zeroes only

I need a javascript regex for validation of numbers that are phone numbers. The numbers cannot be a single zero or only zeroes.

e.g

0
000
00000-000-(000)

these are not allowed.

But these are allowed:

01-0808-000
10(123)(1234)
11111

The javascript regex I have so far is:

  /^[!0]*[0-9-\)\(]+$/

But this does not seem to work.

The rule is the phone number can contain numbers and - and ( and ). It can start with a 0 but the phone number cannot be a single 0 or a number of zeroes only with or without the above characters.

Could you point me in the right direction. Thanks in advance.

like image 224
Shikhar Subedi Avatar asked Apr 07 '14 06:04

Shikhar Subedi


2 Answers

This regex should work:

^(?=.*?[1-9])[0-9()-]+$

Working Demo

like image 107
anubhava Avatar answered Oct 13 '22 01:10

anubhava


Can try this:

/[0-9-()]*[1-9][0-9-()]*/

Will match any number of allowed chars and digits, but if there is no 1-9 anywhere the middle part won't get matched.

/[0-9-()]*[1-9][0-9-()]*/

Regular expression visualization

Debuggex Demo

like image 34
Mosho Avatar answered Oct 12 '22 23:10

Mosho