What is the regular expression for USA toll free number 1 800 ###-####
? How to validate this for phone using c#?
Asked
Active
Viewed 1,806 times
1

Shaharyar
- 12,254
- 4
- 46
- 66
1 Answers
4
You can try this
^(\+?1)?(8(00|33|44|55|66|77|88)[2-9]\d{6})$
Explanation
^ - indicates the beginning of a regular expression (required);
( - indicates the beginning of a regular expression block -
blocks are important to define inner expressions so they can be referenced by the variables $1, $2, $3, etc;
\+1|1? - indicates optional digits '+1' or '1' (the ? sign defines the literal as optional);
) - closes the block;
8 - matches the literal '8';
( - another block starting;
00|33|44|55|66|77|88 - matches 00 or 33 or 44 or 55 or 66 or 77 or 88 (you guessed right, the | sign is the OR operator);
) - closes the inner block;
[2-9] - matches one digit in the range from 2 to 9 (2, 3, 4, 5, 6, 7, 8 and 9),
and as you also guessed the [] pair of brackets encloses a range;
other range examples: [0-9] matches 0 to 9; [a-z] matches a, b, c, ...z);
\d - matches any valid digit (same as [0-9]);
{6} - defines the number of occurrences for the previous expression,
i.e. exactly 6 digits in the range 0-9. This can also contain a variable number of occurrences,
for example to match a sequence of 6 to 9 digits: {6,9};
or to match at least 6 with no maximum: {6,};
) - closes the other block;
$ - indicates the end of the regular expression (required);

Bill Menees
- 2,124
- 24
- 25

Mohit S
- 13,723
- 6
- 34
- 69
-
you could actually write it even smaller: http://regexr.com/3chi7 `^((?:\+?1)?8[04-8]{2}[2-9]\d{6})$` – GottZ Jan 07 '16 at 13:50
-
1@GottZ The smaller expression incorrectly reports 8565551212 as a toll-free number because the [04-8]{2} portion doesn't require the first character to be repeated. It allows any of [04-8] to be followed by any of [04-8]. – Bill Menees Jan 31 '20 at 18:21
-
@BillMenees hm. not so short then: `^(\+?1)?(8([03-8])\3[2-9]\d{6})$` – GottZ Mar 02 '20 at 16:47