Quotes inside ValidationExpression for RegularExpressionValidator

Using the specified control to validate an ASP.NET TextBox, I am curious what a popular practice is. Currently used:

ValidationExpression="^[\w\d\s.,"'-]+$" 

Any shorter way to do this? I tried \ "" no avail. Thanks.

+4
source share
2 answers

Using \" will not work, and you will not be able to use "" . What you have is correct.

However, to make it shorter, you can always use the Unicode: \x22 character escape code equivalent. Even shorter is the octal representation: \42 . Although both are shorter, they do not really help reading. Frequent regular expression users would realize that they represent a certain character, but they may not know which character to not look for. Also, you won’t be able to comment on this unless you plan on leaving comments on ASP.NET markup next to explain the regex.

However, I don’t really like how " . It looks weird and inappropriate, making \x22 or \42 look a little cleaner. Your call.

 ValidationExpression="^[\w\d\s.,\x22'-]+$" ValidationExpression="^[\w\d\s.,\42'-]+$" 

Ultimately, this allows you to reset 2-3 characters.

EDIT: Added an even shorter approach using octal representation.

+7
source

I think setting this value from code (where you have more control over formatting / escaping strings) would be your best bet.

0
source

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


All Articles