1

I am working on regex pattern to validate input, but I am not sure how to include () and - symbols.

Description: The input string doesn't have to be filled out. If its filled out it needs to have exactly 5 numbers and input can't start with 26. Also, input needs to accept parenthesis and dash and they can be placed in any part of the input. I accept max 10 characters

Tried:

(^(?!26)((\d){5}))?

Works only for: - empty input - exactly ten digit number for example 0123456789 - also it rejects if you have 26 at the beginning, for example 2651234567

Also, tried to include - and () but this pattern doesn't work at all

(^(?!26)(\-\(\))((\d){5}))?

Valid inputs:
(12345)--
---12)567)
12333
-1-2--3-45
(()))12345
((12345
(-)65768-
(4)1-2-35

Invalid inputs:
26123---
(26)897---
-26333----
26
26(((())
26------
26--345
26)88-76
267-9

I found discussion A comprehensive regex for phone number validation and it helped but I still can't match exactly my entry.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1282256
  • 183
  • 1
  • 6
  • 16
  • 1
    So max length is 10 characters but only `5` of those can be numbers? But you state about validating phone numbers, which are more that `5` numbers? I'm confused. – hwnd Jun 15 '14 at 00:10
  • @hwnd Yes. Accepts max 10 characters and 5 of them need to be numbers – user1282256 Jun 15 '14 at 00:15
  • 1
    Well if you are accepting 10 characters, can all those characters be numbers? Can you provide `valid` and `invalid` examples? – hwnd Jun 15 '14 at 00:17
  • @hwnd it needs to have exactly 5 digits. I included invalid and valid examples. By saying that it doesn't start with 26 I also mean it doesn't start like (26) or -26-- – user1282256 Jun 15 '14 at 01:08
  • 1
    Try the following: `^(?!([()-]*)2\1*6)(?:[()-]*\d){5}[()-]*$` – hwnd Jun 15 '14 at 01:28
  • I tried your version too but it didn't work out. It was kind of close but I don't know where is the error. Thank you for your help. I appreciate it very much. – user1282256 Jun 15 '14 at 01:45

2 Answers2

2

You're using a Java flavor regex. The correct regex pattern would be:

^(?!26)([-()]*\d){5}[-()]*$

This one will make it so your input cannot start with 26. However, your post did not specify if it could be something like --2)6-218 (it doesn't start with 26, however the first two digits are 26. If this were the case, then you would need:

^(?![-()]*2[-()]*6)([-()]*\d){5}[-()]*$

The 10 character max should be validated on the input, maxlength=10.

Edit: as @zx81 pointed out, I had a few unnecessary escapes. I don't know what I was thinking, sorry. However, this regex pattern does not accept empty strings.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
rion18
  • 1,211
  • 16
  • 22
  • Thank you for good suggestions. It worked I only made one small challenge and added parenthesis, moved $ sign outside of parentheses and added ? at the end since input is not required. – user1282256 Jun 15 '14 at 01:29
  • Among several problems with this regex, it doesn't enforce the 10-char maximum. – zx81 Jun 15 '14 at 01:36
  • @zx81 I included maxlength=10 in the and I can't input more than 10 characters. So it works for me. I tested it. What is the problem with the regex. – user1282256 Jun 15 '14 at 01:57
  • @zx81 I see. My apology in the original post I wrote that it rejects if the input is 555. I meant that it should reject if at the beginning of the number is 26. Anyway, you catched that in your regex pattern. Very good job!!! – user1282256 Jun 15 '14 at 02:00
  • @user1282256 `What is the problem with the regex` Which one?... The first, the second, or the one you modified? Tell me which one you're planning to use, I'll tell you the problems (if any remain.) – zx81 Jun 15 '14 at 02:03
  • @user1282256 The `?` means you would accept an empty string. Apart from that, and the fact that it requires a second validation to check the length... And some unneeded escapes, as in `-`..., and some unneeded capture groups... It can work! :) – zx81 Jun 15 '14 at 02:10
1

This regex will do what you want (see demo):

^(?!\D*(?:26|555))(?=(?:\D*\d){5}\D*$)[\d()-]{5,10}$

If you no longer want to reject 555, you can go with:

^(?!\D*26)(?=(?:\D*\d){5}\D*$)[\d()-]{5,10}$

And if 2--6123 is not allowed, change the regex to

^(?!\D*2[()-]*6)(?=(?:\D*\d){5}\D*$)[\d()-]{5,10}$

Explain Regex

^                        # the beginning of the string
(?!                      # look ahead to see if there is not:
  \D*                    #   non-digits (all but 0-9) (0 or more
                         #   times (matching the most amount
                         #   possible))
  (?:                    #   group, but do not capture:
    26                   #     '26'
   |                     #    OR
    555                  #     '555'
  )                      #   end of grouping
)                        # end of look-ahead
(?=                      # look ahead to see if there is:
  (?:                    #   group, but do not capture (5 times):
    \D*                  #     non-digits (all but 0-9) (0 or more
                         #     times (matching the most amount
                         #     possible))
    \d                   #     digits (0-9)
  ){5}                   #   end of grouping
  \D*                    #   non-digits (all but 0-9) (0 or more
                         #   times (matching the most amount
                         #   possible))
  $                      #   before an optional \n, and the end of
                         #   the string
)                        # end of look-ahead
[\d()-]{5,10}            # any character of: digits (0-9), '(', ')',
                         # '-' (between 5 and 10 times (matching the
                         # most amount possible))
$                        # before an optional \n, and the end of the
                         # string
zx81
  • 41,100
  • 9
  • 89
  • 105
  • 1
    You still want to reject `555` at the beginning, right? Anyhow, please see the demo and let me know if you have questions. :) – zx81 Jun 15 '14 at 01:37
  • If you want to allow `555`, see second option I just added. What about `2--6123`, is that allowed? If not, change the regex to `^(?!\D*2[()-]*6)(?=(?:\D*\d){5}\D*$)[\d()-]{5,10}$` – zx81 Jun 15 '14 at 02:12