I have several pages in my form where I need to validate Email addresses. Rather than set ValidationExpression
in each page , I want to store the Regex string in a Constants class as a string constant and do some thing like
ValidationExpression="<%=Tickets.App_Code.Constants.EmailRegex%>"
I know I can do this in Code Behind but I want to do it declaratively in Aspx page. However the above does not work as the text is not replaced by the contents of the constant. In ASP.Net we have syntax like <%# %>(Binding) , <%= %&开发者_如何学运维gt; (Invoke code)
.
I searched the web but I cannot find much documentation on this area. Can somebody help?
If you declared your class something like this:
namespace MyNamespace
{
public class MyClass
{
public const string EmailRegex = @"\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";
}
}
You can certainly do this and it will work:
<asp:RegularExpressionValidator runat="server" id="myvalidator" ErrorMessage="email does not have correct format" ValidationExpression='<%=MyNamespace.MyClass.EmailRegex %>' ControlToValidate="email"></asp:RegularExpressionValidator>
<asp:TextBox ID="email" runat="server"></asp:TextBox>
Sample output:
精彩评论