Regex to validate length of alphanumeric string
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.
Related videos on Youtube
dtsg
Updated on July 09, 2022Comments
-
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 almost 11 years
if (str.Length > 10)
? -
npinti almost 11 yearsDo 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 almost 11 yearsThis matches a string
AAA AAA
. The input cannot contain more than two spaces in a row -
It'sNotALie. almost 11 yearsThis matches double spaces.
-
dtsg almost 11 yearsAs 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 almost 11 yearsYou 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. almost 11 years@Srb1313711 Try
test test
-
Srb1313711 almost 11 yearsBy double space do mean two in a row or two in the whole string?
-
dtsg almost 11 yearsTwo (or more) consecutive spaces
-
Srb1313711 almost 11 yearsIn 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 almost 11 yearsThe empty check can probably be removed as we have a required field validator on the control anyway
-
Bohemian almost 11 yearsOK, 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 almost 11 yearsYep, that's my bad. Would adding
\w
allow for latin\accented characters too? -
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 almost 11 yearsExcellent. Ending up using:
^(?i)(?!.* )(?=.*[a-z0-9-])[\wa-z0-9 -]{1,10}$
-
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