title: Custom MVC4 validation based on a bool method
body: <p>I have a method that validates a number, I don't have a regular expression for this as it is kind of complicated to create.</p>
  
  <pre><code>public bool IsRegistrationNumberValid(int number)
  {
  ...
  
  }
  </code></pre>
  
  <p>On my form, I have a textbox and I want to add validation for this column.
  How can I create a custom annotation or hook into the ModelState object to add an error message?</p>
  
  <p>My POST controller action is like:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Create(UserRegistrationViewData model)
      {
          if (ModelState.IsValid)
          {
          ...
          }
      }
  </code></pre>
  
  <p>I'm not sure what options I have, can I just create a custom attribute to add to my model?
  And/Or should i just hook into the Model state and add the error message before I check for <code>ModelState.IsValid</code>?</p>