Using RegularExpressionValidator to validate the length of a String

February 15, 2010

This post I’ll keep short and sweet.

If you are using a Regular Expression Validator you will have the problem that if you use a line break it won’t validate when you write something like ^.{0,n}$ even though the dot is supposed to match anything

Apparently the dot isn’t supposed to match new line characters

I took the following from http://weblogs.asp.net/cosgood/archive/2003/05/06/6604.aspx although the wrong bracket is used to close off the cardinality

^(.|\s){0,n}$

That’s it, just remember to escape the backslash depending on where you are setting the expression.

  • Share/Bookmark

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

tags:
posted in Uncategorized by admin

Follow comments via the RSS Feed | Leave a comment | Trackback URL

4 Comments to "Using RegularExpressionValidator to validate the length of a String"

  1. Trget wrote:

    The “dot” is supposed to match anything except the newline character. It’s pretty standard but does seem to catch a lot of people. Even your article mentions this.

  2. Stephen Lacy wrote:

    I mostly just skimmed it looking for the right info. Guess my regular expressions aren’t really up to scratch. Thanks for pointing it out, thought it was just microsoft going off standard

  3. Darxide wrote:

    Trget picked this up immediately. It didn’t click with me till I went and checked my old perl re notes.

    Funnily enough I’ve never had this come up despite my extensive use of regular expressions in word sense disambiguation. I always used \S for crossing newline characters.

  4. admin wrote:

    Yeah, I wasn’t too surprised that it was trget who spotted that. He tends to notice these things.

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org