-3

I want to use regex to validate a phone number. Mobile number must be between six and ten digits and there is no limit to spaces.

For example:

1 2345 false
123 45 false
12345 false
123 45678912 false

1 23456 true
123 456 true
123456 789 1 true
12 345 678 90 true

Now i use: ^[0-9\s]{6,10}$, but this 1 7 is true.

JvdV
  • 70,606
  • 8
  • 39
  • 70
rezanouri
  • 137
  • 2
  • 9

1 Answers1

1

Use

^ *(\d *){6,10}$

See proof

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
   *                       ' ' (0 or more times (matching the most
                           amount possible))
--------------------------------------------------------------------------------
  (                        group and capture to \1 (between 6 and 10
                           times (matching the most amount
                           possible)):
--------------------------------------------------------------------------------
    \d                       digits (0-9)
--------------------------------------------------------------------------------
     *                       ' ' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
  ){6,10}                  end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37