1

in my application i want to validate the telephone numbers, how can i write regular expression for telephone numbers like..

040-23357399 or 04023357399 91-40-23357399 or 914023357399 08518-2814655 or 085182814655 91-8518-2814655 or 9185182814655

this is my phone numbers how can i write the expression for this

HotTester
  • 5,620
  • 15
  • 63
  • 97
Surya sasidhar
  • 29,607
  • 57
  • 139
  • 219
  • 1
    What have you tried to do so far? Have you taken a look around various regular expression website on the web? While someone will give you an answer (because points are good and it's nice to answer questions), it's typically better if you tell us what you've tried first so you can learn something from this as well so you can teach others later. – JasCav Mar 08 '10 at 05:00
  • 1
    Dupe: http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation and probably others. – Sasha Chedygov Mar 08 '10 at 05:01
  • And also near dup of http://stackoverflow.com/questions/1547920/ – Jonathan Leffler Mar 08 '10 at 05:50

2 Answers2

2

Validation implies rejection, and your users will find that annoying. A better option is to accept as much as possible by first stripping all non-digits from the string, and then checking the length and initial digits vs a few simple rules:

  • If 11 digits and starts with a 1, strip first digit.
  • If 10 digits accept if it starts with 2-9.
  • If 12-14 digits accept if starts with a valid country code.
  • Rreject everything else.

This passes a lot more through than trying to force your users into a specific format, and makes for simpler, faster validation code. Then you just format it nicely whenever you re-display it.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

For more refer this

Another good link is this

Also check this regular expression:

^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$
HotTester
  • 5,620
  • 15
  • 63
  • 97