4

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.

2
  • if (str.Length > 10)? Commented Jul 25, 2013 at 9:52
  • 2
    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. Commented Jul 25, 2013 at 9:53

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

6 Comments

The empty check can probably be removed as we have a required field validator on the control anyway
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.
Yep, that's my bad. Would adding \w allow for latin\accented characters too?
\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.
Excellent. Ending up using: ^(?i)(?!.* )(?=.*[a-z0-9-])[\wa-z0-9 -]{1,10}$
|
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.

7 Comments

This matches double spaces.
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
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?
@Srb1313711 Try test test
By double space do mean two in a row or two in the whole string?
|
-1

I think no "+" when using range.

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

Also, you say you accept hyphens, but don't see that here?

So

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

1 Comment

This matches a string AAA AAA. The input cannot contain more than two spaces in a row

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.