0

Can somebody give me the regex for phone number.

The format is :

A phone number used for calls must contain only the characters + , , * and 0-9.

Only the leading character may be a + Or *

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
user1845180
  • 31
  • 2
  • 4
  • I am completely new to this feature hence not sure abt what the expression should be. Basically the length od the phone number should not be more that 30 chararters. – user1845180 Nov 22 '12 at 13:38

1 Answers1

2

The following regex will do:

^\+?[0-9\-]+\*?$

How it works:

  1. Beginning of string: ^
  2. Optional + character, escaped because "+" could also be a regex operator: \+?
  3. At least one character which is either 0-9 or "-", escaped because "-" could also be a regex operator: [0-9\-]+
  4. Optional "*" character, escaped: \*?
  5. End of string: `$'

Note that this is just a simple example to match the pattern as you defined it. A more elaborate discussion on handling US phone numbers with Regex can be found here

Community
  • 1
  • 1
onon15
  • 3,620
  • 1
  • 18
  • 22
  • @onon15 no need to escape the `-` since it's at the end of the class (at the beginning would have been the same by the way). – sp00m Nov 22 '12 at 13:58