Regex to check text box length

I have this RegEx that checks input (in javascript) to make sure the user does not enter more than 1000 characters in the text box:

^.{0,1000}$

It works fine if you enter text on one line, but as soon as you press Enter and add a new line, it will stop matching. How do I modify this RegEx to fix this problem?

+3
source share
3 answers

What you wish:

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

The reason is that .it will not match newlines.

It’s best not to use regular expressions and just use <text area element>.value.length

+3
source

, . . , - :

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

( m), ? .length?

jwz quote:

, , : " , ". .


: CustomValidator, , Regex. MSDN .

+6

If you just want to check the length of the input, would it be easier to just check the length of the string?

if (input.length > 1000)
    // fail validation
+3
source

Source: https://habr.com/ru/post/1790084/


All Articles