title: How can I set a RegularExpression data annotation's regular expression argument at runtime?
body: <p>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):</p>
  
  <pre><code>[Required(ErrorMessage="Email is required")]
  [RegularExpression(@"MYREGEX", ErrorMessage = "Email address is not valid")]
  public string Email { get; set; }
  </code></pre>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>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?</p>