tags:

views:

163

answers:

2

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 avail. Thanks.

A: 

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.

bdukes
I would do that, but on all pages I've used this control, it's done in the .aspx. Seems to me that keeping that consistent is better than making the exception once to save 4-5 characters. Thanks for the idea though.
lush
+3  A: 

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.

Ahmad Mageed