I current have the following regular expression to accept any numeric value that is seven digits
^\d{7}
How do I improve it so it will accept numeric values that are seven or ten digits?
Pass: 0123456, 1234567, 0123456789, 123467890
Fail: 123456, 12345678, 123456789
A simple solution is this:
There are at least two things to note with this solution:
\dmay match far more than you intended (for example foreign characters that are digits in other non-Latin languages).(?: ... ).So for these reasons you may want to use this slightly longer expression instead:
Here’s a little testbed in C# so that you can see it works:
Result: