开发者

Quotes inside ValidationExpression for RegularExpressionValidator

Using said control to validate a ASP.NET TextBox, I'm curious what the most popular practice is. Currently using:

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

Any shorter way of doing this? Tried \ , "" to no av开发者_Python百科ail. Thanks.


Using \" won't work, and you won't be able to use a "" either. What you have matches correctly.

That said, to make it shorter you could always use the unicode character escape equivalent: \x22. Even shorter is the octal representation: \42. While both are shorter, they don't help readability much. Frequent regex users would understand that it represents some character, but they might not know which character without looking it up. In addition, you won't be able to comment it, unless you plan to leave ASP.NET markup comments nearby to explain the regex.

Yet, I don't particularly like how " looks like either. It looks odd and out of place, making \x22 or \42 look a tad cleaner. Your call.

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

Ultimately this lets you shave 2-3 characters off.

EDIT: added an even shorter approach using octal representation.


I think setting that value from the codebehind (where you'll have more control over string formatting/escaping) is going to be your best bet.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜