最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Apr 7, 2014 at 6:12 Shikhar Subedi asked Apr 7, 2014 at 6:06 Shikhar SubediShikhar Subedi 6161 gold badge9 silver badges27 bronze badges 2
  • Could you describe the rules as well? Having examples is good, but doesn't take into consideration the majority of formats. – Jerry Commented Apr 7, 2014 at 6:08
  • so 00000000001 is allowed? – aelor Commented Apr 7, 2014 at 6:11
Add a comment  | 

4 Answers 4

Reset to default 7

This regex should work:

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

Working Demo

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-()]*/

Debuggex Demo

IMO you should do something like this :

var str = "10(123)(1234)";
var res = str.replace(/[^\d]/g, '');
var fres = /^0+$/.test(res);
if(fres)
  console.log("Not a valid phone number");
else
  console.log("valid phone number");

this will tell you whether your phone number is valid or not based on the content of zeroes. If all zeroes and no other digit is present, then it will return true else false

This one:

(?=^[0-9-)(]+$)(?=.*[1-9].*)
发布评论

评论列表(0)

  1. 暂无评论