Friday, November 4, 2016

RequiredExpressionValidator expression to limit the number of characters entered in a textbox

For the ASP.NET project that I'm currently working on, I wanted to limit the number of characters that could be entered into a specific textbox control to a maximum of 25 characters (NOTE: since this was a required field so I also wanted them to at least enter 1 character); however, in this particular instance, I wanted to allow the user to be able to enter any kind of special character.  To provide this capability, I used the following value for my ValidationExpression:

^.{1,25}$

Now, if I did want to limit the type of characters entered to be only alpha-numeric and empty spaces, I used the following:

^[a-zA-Z0-9 ]{1,25}$