Regular Expression Validation For Indian Phone Num

2019-01-11 04:23发布

I want to validate Indian phone numbers as well as mobile numbers. The format of the phone number and mobile number is as follows:

For land Line number

03595-259506
03592 245902
03598245785

For mobile number

9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389

I would like the regular expression in one regex. I have tried the following regex but it is not working properly.

{^\+?[0-9-]+$}

8条回答
手持菜刀,她持情操
2楼-- · 2019-01-11 04:30

This depends a lot on what you want the regex for: if you're importing from a rather dirty database, you might want a liberal expression that just weeds out the completely wrong ones. This one would be enough for the following case:

^\+?[\d(). -]{5,25}

The phone number:

  • may or may not start with a plus.
  • could have one of these characters: ( ) . - space digit.
  • should have between 5 to 25 characters.

(Side Note: According to ITU, phone numbers are limited to max of 15 digits (See Phone Numbering Plan). A max=25 in above Regex accommodates the special characters.)

Visual Explaination

enter image description here

查看更多
够拽才男人
3楼-- · 2019-01-11 04:35

This works really fine:

\+?\d[\d -]{8,12}\d

Matches:

03598245785
9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389
987-98723-9898
+91 98780 98802
06421223054
9934-05-4851
WAQU9876567892
ABCD9876541212
98723-98765

Does NOT match:

2343
234-8700
1 234 765
查看更多
狗以群分
4楼-- · 2019-01-11 04:43

For both mobile & fixed numbers: (?:\s+|)((0|(?:(\+|)91))(?:\s|-)*(?:(?:\d(?:\s|-)*\d{9})|(?:\d{2}(?:\s|-)*\d{8})|(?:\d{3}(?:\s|-)*\d{7}))|\d{10})(?:\s+|)

Explaination:

(?:\s+|)                    // leading spaces
((0|(?:(\+|)91))            // prefixed 0, 91 or +91
(?:\s|-)*                   // connecting space or dash (-)
(?:(?:\d(?:\s|-)*\d{9})|    // 1 digit STD code & number with connecting space or dash
(?:\d{2}(?:\s|-)*\d{8})|    // 2 digit STD code & number with connecting space or dash
(?:\d{3}(?:\s|-)*\d{7})|    // 3 digit STD code & number with connecting space or dash
\d{10})                     // plain 10 digit number
(?:\s+|)                    // trailing spaces

I've tested it on following text

9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389

0359-2595065
0352 2459025
03598245785
07912345678
01123456789
sdasdcsd
+919898101353
dasvsd0 
+91 dacsdvsad
davsdvasd

0112776654
查看更多
贼婆χ
5楼-- · 2019-01-11 04:45

You Can Use Regex Like This:

   ^[0-9\-\(\)\, ]+$
查看更多
我想做一个坏孩纸
6楼-- · 2019-01-11 04:50
var phonereg = /^(\+\d{1,3}[- ]?)?\d{10}$/;
查看更多
闹够了就滚
7楼-- · 2019-01-11 04:51

For land Line Number

03595-259506
03592 245902
03598245785

you can use this

\d{5}([- ]*)\d{6}

NEW for all ;)

OLD: ((\+*)(0*|(0 )*|(0-)*|(91 )*)(\d{12}+|\d{10}+))|\d{5}([- ]*)\d{6}
NEW: ((\+*)((0[ -]+)*|(91 )*)(\d{12}+|\d{10}+))|\d{5}([- ]*)\d{6}

9775876662
0 9754845789
0-9778545896
+91 9456211568
91 9857842356
919578965389

03595-259506
03592 245902
03598245785

this site is useful for me, and maby for you .;)http://gskinner.com/RegExr/

查看更多
登录 后发表回答