user194076
user194076

Reputation: 9027

Regular expression: limit number of characters

I have a textbox and need to limit number of characters in it (no more than 1000).
This is the regular expression i have: ^.{0,1000}$
it works great until there is a newline in a textbox (when I hit enter). If there is newline in textbox it shows me the warning "you have more than 2000 characters". Is there workaround of this issue.

Upvotes: 1

Views: 519

Answers (2)

SLaks
SLaks

Reputation: 888213

Set the RegexOptions.SingleLine flag.

Alternatively, you could explicitly include newlines in the regex: ^(\n|.){0,1000}$

Upvotes: 1

bdukes
bdukes

Reputation: 156055

. doesn't match all characters (i.e. newlines), so you can use two characters groups to get all characters, like this:

^[\s\S]{0,1000}$

Upvotes: 3

Related Questions