RegEx Max and Min length characters for text field

I am using Asp.net and C #.

I need to force the user to add only text from 4 to 128 characters to the TextBox Control.

I would like to use the ValidationExpression property to control validation.

Could you specify the correct regular expression ?

Notes. I am using this code right now, but it does not seem to work properly if there are two spaces or line breaks in the TextBox text box

ValidationExpression="^.{4,128}$" 

Thank you for your time!

+2
source share
3 answers

Your expression is correct. Just use the Singleline modifier so that the dot also matches newlines.

 RegexOptions.Singleline 

Or as a built-in modifier

 "^(?s)(.){4,128}$" 

Enumeration RegexOptions
Regex options

+4
source

The symbol for a complete stop or period (.) Is called a dot. This is a wildcard character that matches any character except a newline (\ n).

Link: http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx


Try this instead:

 ValidationExpression = "^(.|\n|\t){4,128}$" 

I added tabs ( \t ).

Tell me if it worked or not!

+2
source

try this ValidationExpression = ^(\w*)(\s*)(.*){4,128}$" , it will also cover periods and spaces.

0
source

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


All Articles