Shawn
Shawn

Reputation: 869

C# RegularExpressionValidator Trim and Count

I have a textbox with a RegularExpressionValidator. I want to require the user to enter at least n characters. I'd also like to remove whitespace both at the start and end of the textbox. I'd still like to allow spaces within the textbox, I just want to remove the excess at the beginning and end.

I basically don't know how to combine the trim regex and the count together for use in a REV.

trim: ^\s*((?:[\S\s]*\S)?)\s*$
count: .{10}.*

I basically want to know if the input, after leading and trailing whitespace is removed, is greater than n characters.

Upvotes: 1

Views: 1039

Answers (1)

Ahmad Mageed
Ahmad Mageed

Reputation: 96477

You could use word boundaries to ignore the whitespace in the beginning, accept 10 characters, then end with another word boundary with a pattern like this:

\b.{10}\b

Be sure to also use a RequiredFieldValidator to cover empty inputs since the RegularExpressionValidator does not handle such cases.

Upvotes: 1

Related Questions