-3

What would be the regex code to validate a number, the number should only be read if it is in between 10-13 digits

I'm using this Regex: /\d([0-9]{11,13})\z/

The problem is it should only accept digits between 11 and 13 if it exceeds 13 it still accepts it.

I want it to simply validate these kind of numbers:

12345678910 = 11 Digits 123456789101 = 12 Digits

Sabih
  • 19
  • 1
  • 5
  • You need to anchor the match from the beginning to the end./ Something such as `/^\d([0-9]{11,13})$\z/` (notice the `^` and `$`) – Cid May 05 '20 at 10:03
  • Does this answer your question? [How to validate phone numbers using regex](https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex) – lurker May 05 '20 at 11:25

1 Answers1

2

Your Regex matches the following:

  • \d, that is a number character
  • followed by 11, 12 or 13 characters from the range of 0-9
  • followed by the end of the string

As such, your regular expression currently matches a numeric string with between 12 and 14 numeric chars at the end. It does not match a numeric string containing only 11 characters. Anything at the start of the string before that is ignored (i.e. your regex would also match foobar 1234567891012)

To fix your regex, you could simplify it to

/\A\d{10,13}\z/

This regex would match the following:

  • the start of the string
  • followed by 10 to 13 numeric characters
  • followed by the end of the string
Holger Just
  • 52,918
  • 14
  • 115
  • 123