Archive for May, 2008

Using the RegularExpressionValidator for validating length of text

Thursday, May 22nd, 2008 | .NET | 4 Comments

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.

Tags:

Search

Knut Hamang

This is the official site for Knut Hamang. I am a professional technical consultant specialized in Microsoft and .NET technologies and platforms. I work as an independent consultant in Norway and have more than 10 years of professional experience. To get in contact with me, please send me an email or check my public profile on View Knut Hamang's profile on LinkedIn