Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to validate textbox length

I have this RegEx that validates input (in javascript) to make sure user didn't enter more than 1000 characters in a textbox:

^.{0,1000}$

It works ok if you enter text in one line, but once you hit Enter and add new line, it stops matching. How should I change this RegEx to fix that problem?

like image 648
Andrey Avatar asked Sep 20 '25 06:09

Andrey


1 Answers

The problem is that . doesn't match the newline character. I suppose you could use something like this:

^[.\r\n]{0,1000}$

It should work (as long as you're not using m), but do you really need a regular expression here? Why not just use the .length property?

Obligatory jwz quote:

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.


Edit: You could use a CustomValidator to check the length instead of using Regex. MSDN has an example available here.

like image 183
Donut Avatar answered Sep 22 '25 20:09

Donut