6

I need to check for users that are putting their id in the wrong input box.

They might enter the id as 123-456-789-012 or 123456789012 or some variation so I can't just check for digits. The length of the id varies slightly per user, but is always more than 10 digits.

Valid input is a mix of characters and 0-10 digits.

I've seen a lot of solutions for plain digits, but not mixed text. I tried variations of

(\D*\d){0,10}

but that didn't work.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
thchaver
  • 331
  • 5
  • 14
  • 2
    Why not "remove all non-digits" and count the digits? – user2864740 Jul 25 '18 at 21:23
  • (\d|[a-z]|-) do this it will match all digits or alphabets or '-' and you can check if the length of the input string is equal to the length of the captured group – Inder Jul 25 '18 at 21:36
  • I have a version that strips out the non-digits and counts what's left, but I'd prefer one step so I can use a RegularExpressionValidator, rather than a CustomValidator. – thchaver Jul 27 '18 at 13:09

1 Answers1

8

You want "0-n non-digits" then "1-10 lots of a digit-and-any-non-digits":

^\D*(\d\D*){1,10}$
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I want less than 11. a most 10 characters is fine. More than 10 is invalid. Your expression worked perfectly in my RegularExpressionValidator to require at least 10, but when I tried switching "10," to ",11", it just invalidated everything. – thchaver Jul 27 '18 at 13:41
  • @thchaver as per my previous answer, if you want exactly 10 remove the comma. I have edited out both the comma and the comment regarding that. Try the current version of my regex – Bohemian Jul 27 '18 at 17:37
  • Thank you for your help. I want fewer than 11 digits. For example, "John", "John1", and "John0123456789" are all valid. "John: 0123-4567-890" is invalid. – thchaver Jul 27 '18 at 19:00
  • @thchaver it now allows between 1 and 10. – Bohemian Jul 27 '18 at 21:18
  • I changed the 1 to a 0 and it worked perfectly. Thanks! – thchaver Jul 30 '18 at 13:25