Validators
Using the RegularExpressionValidator for validating length of text
The RegularExpressionValidator can be used for almost everything regarding validation of input fields. My favourites are validating Email, Dates and length of text in textareas. The only problem with the length validation, is validating text that includes newline. One would think that the regex: .{0,4000} would be an easy way to validate maximum length of 4000 characters. That is true, but the dot does not include NewLine (CheatSheet). The RegularExpressionValidator also does NOT have an option for parsing the text as single line, which is a common Regular Expression option.
Reading another blog I found an expression that includes newlines: ^(.|\s){0,n)$
The only problem with this is that IE 6/7 and FF freezes when trying to test the expression. Seems like this is a JavaScript/ECMAScript problem. Disabling the clientscript for the RegularExpressionValidator however would probably solve this. The expression works fine in RegexCoach.
A comment in the blog suggested in using: ^[\s\S]{0,n}$
This expression seems to work fine in the browser. Also seems to work fine with number of characters. So I think I will stick with that for now.
Search
Knut Hamang
Recent Posts
Recent Comments
- Lori on Html to Pdf in .NET
- Susan on Html to Pdf in .NET
- Susan on Html to Pdf in .NET
- Ananth on Html to Pdf in .NET
- Tim M on Passing an object to ObjectDataSource