TBA
TBA

Reputation: 1187

Regular Expression Asp.Net MVC

I was trying to get a user name validations where the following texts are valid,

But not the following

I added following annotations in Model, though does't seem to be working for the correct texts too. Keep getting the error message.

[RegularExpression(@"^[a-zA-Z0-9\-_]$", ErrorMessage = "Only alphanumeric, hyphen and underscores are allowed for User name.")]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]

Upvotes: 2

Views: 70

Answers (1)

anubhava
anubhava

Reputation: 785058

From your examples it looks like this regex can be suitable for you:

@"^[a-zA-Z0-9_-]{4,10}$"

{4,10} sets minimum length of input to 4 and max length to 10. Change it whatever limits you want to keep.

Upvotes: 2

Related Questions