I have this in an ascx file:
<asp:textbox ID="AddHourlyRate" runat="server" width="55px" />
<asp:RegularExpressionValidator ID="AddHourlyRateRegexValidator"
ErrorMessage="Please enter a valid charge rate (whole number only)"
ValidationExpression="^\$?[0-9]+(\.[0]{2})?$" ControlToValidate="AddHourlyRate"
Display="Dynamic" runat="server" />
How can I reuse the ValidationExpression in other asp:RegularExpressionValidators without having to copy-paste the regex expression? I am new to asp.NET.
Make a class with const string for each regular expression, I use that solution for your exact problem. That class is in a common assembly that is shared among number of projects. And you probably use regular expression on some other places not just validator controls.
Something like this :
You can then set validator expression from code behind :
or in markup :
Of course write proper namespace in import directive.