Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:53:50+00:00 2026-05-27T12:53:50+00:00

We manage several ASP.NET MVC client web sites, which all use a data annotation

  • 0

We manage several ASP.NET MVC client web sites, which all use a data annotation like the following to validate customer email addresses (I haven’t included the regex here, for readability):

[Required(ErrorMessage="Email is required")]
[RegularExpression(@"MYREGEX", ErrorMessage = "Email address is not valid")]
public string Email { get; set; }

What I would like to do is to centralise this regular expression, so that if we make a change to it, all of the sites immediately pick it up and we don’t have to manually change it in each one.

The problem is that the regex argument of the data annotation must be a constant, so I cannot assign a value I’ve retrieved from a config file or database at runtime (which was my first thought).

Can anyone help me with a clever solution to this—or failing that, an alternative approach which will work to achieve the same goal? Or does this just require us to write a specialist custom validation attribute which will accept non-constant values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:53:50+00:00Added an answer on May 27, 2026 at 12:53 pm

    The easiest way is to write a custom ValidationAttribute that inherits from RegularExpressionAttribute, so something like:

    public class EmailAttribute : RegularExpressionAttribute
        {
            public EmailAttribute()
                : base(GetRegex())
            { }
    
            private static string GetRegex()
            {
                // TODO: Go off and get your RegEx here
                return @"^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$";
            }
        }
    

    That way, you still maintain use of the built in Regex validation but you can customise it. You’d just simply use it like:

    [Email(ErrorMessage = "Please use a valid email address")]
    

    Lastly, to get to client side validation to work, you would simply add the following in your Application_Start method within Global.asax, to tell MVC to use the normal regular expression validation for this validator:

    DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I manage a web application for a client with the following specs: ASP.net 3.5
I need to share those types of resources across several ASP.NET web sites. The
I have simple web application (asp.net - ajax) I have a web form which
I manage a large asp.net site which has previously been converted from static html
I have an Asp.NET MVC site that I manage multiple instances of. Each instance
Short Version : I'm using a 64bit dll (system.data.sqlite) in an Asp.net MVC app
After developing ASP.NET apps exclusively for several years I'm about to start developing Winforms
I've looked at several URL rewriters for ASP.Net and IIS and was wondering what
I have a web application deployment project setup for an ASP.NET application with Visual
ASP.NET MVC makes it really easy to create editing templates for simple, flat objects.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.