Regex to validate length of alphanumeric string

21,527

Solution 1

I would do it like this:

^(?!.*  )(?=.*[\w-])[\w -]{1,10}$

This uses a negative look-ahead (?!.* ) to assert there are not two consecutive spaces, and a positive look-ahead (?=.*[\w-]) to assert it has at least one non-space character (I assume "empty" means "only spaces").

Note that if it can't be "empty", it can't be zero length, so the length range must be 1-10, not 0-10.

Of tiny note is the fact that you don't need to escape the dash in a character class if it's the first or last character.

Solution 2

(?i)([a-z?0-9?\-?]\s?){0,10}

Case insensitive, between 0-10 length, matches any combination of letters, numbers, hyphens and single spaces.

Share:
21,527

Related videos on Youtube

dtsg
Author by

dtsg

Updated on July 09, 2022

Comments

  • dtsg
    dtsg almost 2 years

    I have the following regular expression:

    ^[a-zA-Z0-9]+( [a-zA-Z0-9]+)*$
    

    I'm trying to validate a string between 0-10 characters, the string cannot contain more the two spaces in a row or cannot be empty. The string cannot contain any special characters and can be case insensitive and can include hyphens.

    How can I limit input to between 0-10 characters?

    I tried

    ^[a-zA-Z0-9]+( [a-zA-Z0-9]+{0,10})*$
    

    but it does not work.

    • Feidex
      Feidex almost 11 years
      if (str.Length > 10)?
    • npinti
      npinti almost 11 years
      Do not try to do everything with one regular expression... that will most likely drive you up a wall. For length, you can use the .length property provided by the .NET framework. I'd recommend you use multiple regular expressions to perform smaller, much less complex tasks.
  • dtsg
    dtsg almost 11 years
    This matches a string AAA AAA. The input cannot contain more than two spaces in a row
  • It'sNotALie.
    It'sNotALie. almost 11 years
    This matches double spaces.
  • dtsg
    dtsg almost 11 years
    As mentioned above, this matches double spaces. Almost there though: (?i)\w([a-z?0-9?\-?]\s?){0,10} added \w to account for accented characters é as i believe these are allowed
  • Srb1313711
    Srb1313711 almost 11 years
    You sure this matches double space i tested in a few testers and it wont match more than one space? Would you like a link to the testers?
  • It'sNotALie.
    It'sNotALie. almost 11 years
    @Srb1313711 Try test test
  • Srb1313711
    Srb1313711 almost 11 years
    By double space do mean two in a row or two in the whole string?
  • dtsg
    dtsg almost 11 years
    Two (or more) consecutive spaces
  • Srb1313711
    Srb1313711 almost 11 years
    In which case im still consfused? Try this tester gskinner.com/RegExr with the regex i supplied, if more than one space it will stop matching is this not what you want?
  • dtsg
    dtsg almost 11 years
    The empty check can probably be removed as we have a required field validator on the control anyway
  • Bohemian
    Bohemian almost 11 years
    OK, but the question did call for regex assertion that there was at least one non-blank. In case someone else comes along later to use this answer, they may not have that extra layer of checking.
  • dtsg
    dtsg almost 11 years
    Yep, that's my bad. Would adding \w allow for latin\accented characters too?
  • Bohemian
    Bohemian almost 11 years
    \w includes all "letters", "digits" and the underscore character. "Letters" means any unicode "letter" (includes accents, greek, arabic, the works). "Digits" includes arabic, chinese "numbers" too.
  • dtsg
    dtsg almost 11 years
    Excellent. Ending up using: ^(?i)(?!.* )(?=.*[a-z0-9-])[\wa-z0-9 -]{1,10}$
  • Bohemian
    Bohemian almost 11 years
    \w includes a-zA-Z0-9. I've updated the answer. It's a lot simpler now. Hopefully it still works for you