Taimoor
Taimoor

Reputation: 81

Create REGEX of string having 11 to 13 digits

Creating a regex for phone number that accepts 11 to 13 digits and + in start " - ( ) and _" within the string.

Here is a regex which i have created that accepts till 11 digits but max isn't set:

^((\(?\+?[0-9]*\)?)?[0-9_\- \(\)]){11,13}$

Thanks in advance.

Upvotes: 0

Views: 829

Answers (1)

revo
revo

Reputation: 48711

You could use a positive lookahead to apply the limitation on digits. Then write your pattern to match the actual format:

^(?=\+?(?:\d-?){11,13}$)\+?\d+(?:-\d+)*$

Regex breakdown:

  • ^ Start of input string
  • (?= Start of positive lookahead
    • \+? Match an optional +
    • (?:\d-?){11,13} Match between 11 to 13 digits, allow dashes
    • $ End of input string
  • ) End of lookahead
  • \+?\d+ Match + optionally then a sequence of digits
  • (?:-\d+)* Match any number of -\d+ occurrences
  • $ End of input string

See live demo here

Upvotes: 1

Related Questions