I was trying to get a user name validations where the following texts are valid,
- Test
- Test2
- Tes2
- Test123
- 123Test
- 1234
- Test_56
- test-6ty
But not the following
- Tes (Min Length)
- Test@123 (Special char)
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)]
ModelStateis valid when you submit with either "Tes" or "Test@123"?^[a-zA-Z0-9\-_]*$(or^[a-zA-Z0-9\-_]{4, 100}$to combine both as suggested in anubhava's answer