I have the following code to validate usernames for an application:
Regex usernameRegex = new Regex("[A-Za-z0-9_]");
if (usernameRegex.IsMatch(MyTextBox.Text)) {
// Create account, etc.
}
How would I modify my regular expression to check if the username has a certain number of characters?
This expression validates only all text which contains any combination of
A to Z,a to zand number0 to 9. You can define the length of the string using the regex:{3,}and{2,}mean here that the string must have at least 3 capital characters, at least 2 small characters, and any amount of digit characters.For example :
Valid : AAAbb, AAAbb2, AAAAAAbbbbb, AAAAAbbbbbb4343434
Invalid: AAb, Abb, AbAbabA, 1AAAbb,