Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegularExpressionValidator to limit input length with no limitations on character types

I'm trying to use a RegularExpressionValidator to validate a TextBox input length. I have it working, but it only accepts letters and numbers. I'd like to allow any characters, with the only check being there are no more than 25 characters.

<asp:TextBox ID="MenuLabel" runat="server" />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9]{25}$" ID="MenuLabelVal" runat="server" ErrorMessage="Menu Label must be no longer than 25 characters." ControlToValidate="MenuLabel"  />

Regular expressions are not my strong suit, so can someone tell me how I modify ^[a-zA-Z0-9]{25}$ to represent any characters up to 25 times rather than just alpha-numeric.

Note 1: I already have a RequiredFieldValidator in place to ensure 1 or more characters.

Note 2: I know I could just use the MaxLength property on the TextBox, however this form is also for editing existing data, and I do not want it to simply truncate existing records when editing. I'd prefer to implement a validator which makes it obvious for users editing existing data they need to shorten the value, rather than the form truncating without the user realizing.

Note 3: I'm open to alternate solutions, such as a custom validator, as long as it relies only on client-side validation. I don't have access to the code-behind to write a custom validate handler on the server.

like image 814
KP. Avatar asked Jun 29 '12 14:06

KP.


1 Answers

.{1,25} 

should limit the value to any char 1 to 25 times. Not sure how this will deal with unicode input though - it may count multi-byte chars as a single char, so be careful if you are using this to limit for a database

possibly you might want ^.{1,25}$ but I think even this simple version should work

like image 165
Jason Coyne Avatar answered Oct 21 '22 06:10

Jason Coyne