You need a regular expression to validate your username.

A regular expression is required to validate a username that:

  • must allow spaces, but not spaces between characters
  • must contain at least one letter, may contain letters and numbers
  • Maximum 7-15 characters (alphanumeric)
  • cannot contain special characters
  • underline allowed

I don’t know how to do it. Any help is appreciated. Thank.

This is what I used, but it allows a space between characters

"(?=.*[a-zA-Z])[a-zA-Z0-9_]{1}[_a-zA-Z0-9\\s]{6,14}"

Example: username No spaces are allowed in the username

+5
source share
1 answer

Try the following:

foundMatch = Regex.IsMatch(subjectString, @"^(?=.*[a-z])\w{7,15}\s*$", RegexOptions.IgnoreCase);

Also allows you to use _since you resolved this when trying.

, . , . , , _ , , 7 15 . . , :)

:

    "
^           # Assert position at the beginning of the string
(?=         # Assert that the regex below can be matched, starting at this position (positive lookahead)
   .        # Match any single character that is not a line break character
      *     # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   [a-z]    # Match a single character in the range between "a" and "z"
)
\w          # Match a single character that is a "word character" (letters, digits, etc.)
   {7,15}   # Between 7 and 15 times, as many times as possible, giving back as needed (greedy)
\s          # Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
$           # Assert position at the end of the string (or before the line break at the end of the string, if any)
"
+4

All Articles