1

I am not very experienced with regex and I need to validate phone numbers using javascript. I have a textbox which need to be allowed to accept multiple phone numbers with a delimiter of ';' and the characters that can be allowed for the phone numbers are

  1. Numbers
  2. '+'
  3. '-'

Could someone help me on how I can acheive this using javascript and regex/ regular expressions?

Example:

+91-9743574891;+1-570-456-2233;+66-12324576

I tried the following:

^[0-9-+;]+$

Am not sure if this is correct.

stema
  • 90,351
  • 20
  • 107
  • 135
Abishek
  • 11,191
  • 19
  • 72
  • 111
  • Have you tried anything yet? – sp00m Nov 21 '12 at 13:35
  • @sp00m, I tried the following ^[0-9-+;]+$ – Abishek Nov 21 '12 at 13:36
  • Have a look here for starters: [http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – garyh Nov 21 '12 at 13:37

4 Answers4

1

You have placed - in wrong place so, your regex is not working.

Try this(your RegEx, but slightly modified):

^[0-9+;-]+$

or

^[-0-9+;]+$

To include a hyphen within a character class then you must do one of the following:

  1. escape the hyphen and use \-,
  2. place hyphen either at the beginning or at the end of the character class.

As the hyphen is used for specifying a range of characters. So, regex engine understands [0-9-+;]+ match any of the characters between 0 to 9, 9 to +(all characters having decimal code-point 57[char 9] to 43[char +] and it fails) and ;.

Cylian
  • 10,970
  • 4
  • 42
  • 55
  • I know why, but for the OP you should expand on that a bit. – PointedEars Nov 21 '12 at 13:39
  • You have explained what to do but not why. The reason that `-` needs to be treated special is that the `-` in a character class delimits start and end of a character (and, in ECMAScript, code point) range. – PointedEars Nov 21 '12 at 13:50
  • @PointedEars: I have missed something. Thank you. – Cylian Nov 21 '12 at 14:00
  • 1
    I aggree that this is the way the hyphen should be placed. Here it would not have been a problem (by accident) because a range can not be the start of another range. The 9 is the ending point of the first range and can therefore not be the start point of another range. So this is a special case, where the `-` also has no special meaning but this is not easy to see, so it should not be used this way. (At least this is the way it is handled in the most regex flavours, I am not 100% sure for javascript) – stema Nov 21 '12 at 14:46
  • @stema Exactly (except for "javascript"). Because [the longest match wins](http://ecma-international.org/ecma-262/5.1/#sec-7), `0-9-` is produced by [`NonEmptyClassRanges :: ClassAtom - ClassAtom ClassRanges`](http://ecma-international.org/ecma-262/5.1/#sec-15.10.2.15), where `0` and `9` are the two `ClassAtom`s, leaving `-` to be produced by [`ClassRanges :: NonEmptyClassRanges`](http://ecma-international.org/ecma-262/5.1/#sec-15.10.2.14), `NonEmptyClassRanges :: ClassAtom` (ibid.), and finally [`ClassAtom :: -`](http://ecma-international.org/ecma-262/5.1/#sec-15.10.2.17). – PointedEars Nov 21 '12 at 17:40
0

How about this ^([0-9\-\+]{5,15};?)+$

Explanation:

^          #Match the start of the line
[0-9\-\+]  #Allow any digit or a +/- (escaped)
{5,15}     #Length restriction of between 5 and 15 (change as needed)
;?         #An optional semicolon
+          #Pattern can be repeat once or more
$          #Until the end of the line

Only as restrictive as specified could be tighter, See it working here.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
0

To be a bit more restrictive, you could use the following regexp:

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

What it will match:

+91-9743574891
+1-570-456-2233;+66-12324576

What it won't match:

91-9743574891
+15704562233
6612324576
sp00m
  • 47,968
  • 31
  • 142
  • 252
0

Your regex will match what you allow, but I would be a bit more restrictive:

^\+?[0-9-]+(?:;\+?[0-9-]+)*$

See it here on Regexr

That means match an optional "+" followed by a series of digits and dashes. Then there can be any amount of additional numbers starting with a semicolon, then the same pattern than for the first number.

stema
  • 90,351
  • 20
  • 107
  • 135