1

It's type attribute is set to string and it's min value = 0 and the max value = 100.

I want to insure that the data entered won't exceed the column length defined in the database.

Now when I test it, it always display the error message even if I entered one letter or two!

Mazen Elkashef
  • 3,430
  • 6
  • 44
  • 72

2 Answers2

3

You should use a RegularExpressionValidator for this. RangeValidators aren't for string lengths. Set the ValidationExpression to something like .?, or you can narrow it to something like \S? or \w? or something if you want.

Also, if you're working with a TextBox that's not in MultiLine mode (and there's no reason it would be if the max is one character) you can just set MaxLength="1" on the control and you won't need a validator at all.

EDIT: If you do want to specify a max length greater than one on a multiline TextBox, you can use a RegularExpressionValidator as described above, but set ValidationExpression=".{0,100}" for a maximum length of 100. More information on regex quantifiers is available here.

If you have a single-line TextBox, just set its MaxLength attribute to the desired maximum, and it won't allow anything longer than that; no validator required.

If you want to do anything involving real-time detection of the length as the user types it, that's more complicated and you'll need JavaScript. You should also still validate the input server-side, because any user with JavaScript turned off will be able to bypass client-side validation.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104
  • @Justin Morgan .. Ok, now I get that RangeValidator won't help me with the string length. What I want to to do is something like the comment box of stackoverflow .. it restricts that the max lenght of the comment is 600 (any non english character like unicode still counts as one and spaces counts as a character) and it validates this on both client and server side .. do you think I can do the same ? – Mazen Elkashef Apr 13 '11 at 03:38
  • @Justin Morgan .. About your edit, I'm having two requirement on a single line textbox a max of 100 characters and on a multiline textbox a max of 500 characters. – Mazen Elkashef Apr 13 '11 at 03:42
  • @Justin Morgan .. The MaxLength will work but it won't interact with the user to tell explains why he can't type more than 10 characters for example .. I Tried the Regex solution it worked like a charm (Y) Thanks You =) – Mazen Elkashef Apr 14 '11 at 01:50
  • @Justin Morgan .. I'm sorry I know this is a little off the question's topic but I realized that you love Regular Expressions so I was hoping to tell me how to constraint two separate cases: 1.No spaces at all and the length can be from 0 to 50 2.no spaces in the beginning nor the end, and the length range from 0 to 50 .. Thankful =D – Mazen Elkashef Apr 14 '11 at 02:13
  • @IKashef - Sure, I think I know what you're looking for. Can you post it as a question and put a link to it here? – Justin Morgan - On strike Apr 14 '11 at 03:52
  • Thanks =) http://stackoverflow.com/questions/5676533/whats-the-regex-for-no-spaces-allowed-in-asp-net – Mazen Elkashef Apr 15 '11 at 12:15
1

Set the "MaxLength" property on the control to 1. This MSDN page describes the property and its usage.

RangeValidator validates the value of the input not the length, therefore you are limiting the valid inputs to {0, 1} with your validator.

Dan
  • 1,489
  • 12
  • 16
  • . I want the length of the input string to be between 0 and 100. How setting the MaxLength to 1 will help ? – Mazen Elkashef Apr 13 '11 at 03:43
  • @IKashef - I stated set the MaxLength poperty to one because you stated you wanted the field length to be "min value = 0 and the max value = 1". If you want MaxLength to be 100 then set it to 100 – Dan Apr 13 '11 at 04:12
  • . You're right I mistyped the max value I meant 100 .. anyway both the answers are right but Justin Morgan's is more accurate since you have to add a dot to the expression .. "{0, 1}" won't work .. +1 Thanks for your time and good solutions :) – Mazen Elkashef Apr 14 '11 at 01:52