title: ASP.NET MVC 4 Custom Validation not working
body: <p>I am trying to use a custom validation but its been ignored. I do not get any errors or anything. It simply does nothing. What am i doing wrong? Thank you for reading. </p>
  
  <p>---<strong>ModelMetadata.cs</strong>----</p>
  
  <pre><code>using System.ComponentModel.DataAnnotations; 
  using myproject.Common;
  
  namespace myproject.Models
  {
      [MetadataType(typeof(ModelMetaData))]
      public partial class Reference
      { 
      }
  
      public class ModelMetaData { 
          [Required]
          [Display(Name = "Full Name *")]
          [ExcludeCharacters("/.,!@#$%")] // &lt;&lt;&lt;====== This is being ignored. 
          public string Name { get; set; } 
      }
  }
  </code></pre>
  
  <p>--<strong>Create.cshtml</strong> ----</p>
  
  <pre><code>&lt;script src="~/Scripts/jquery-1.8.2.min.js"&gt;&lt;/script&gt;
  &lt;script src="~/Scripts/jquery.validate.js"&gt;&lt;/script&gt; 
  &lt;script src="~/Scripts/jquery.validate.unobtrusive.min.js"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()){
  @Html.AntiForgeryToken()
  @Html.ValidationSummary(true) 
  
      &lt;div class="editor-field"&gt;
          @Html.EditorFor(model =&gt; model.Name) // &lt;&lt;&lt;====== field 
          @Html.ValidationMessageFor(model =&gt; model.Name)
      &lt;/div&gt;
  </code></pre>
  
  <p>--<strong>ExcludeCharacters.cs</strong> --- </p>
  
  <pre><code>namespace myproject.Common
  {
      public class ExcludeCharacters : ValidationAttribute 
      {
          private readonly string _chars;
  
          public ExcludeCharacters(string chars) : base("{0} contains invalid characters")
          {
              _chars = chars;
          }
  
          protected override ValidationResult IsValid(object value, ValidationContext validationContext)
          {
              if (value != null)
              {
                  for (int i = 0; i &lt; _chars.Length; i++)
                  {
                      var valueAsString = value.ToString();
                      if (valueAsString.Contains(_chars[i].ToString()))
                      {
                          var errorMessage = FormatErrorMessage(validationContext.DisplayName);
                          return new ValidationResult(errorMessage);
                      }
                  }
              }
              return ValidationResult.Success;  
          }  
      } 
  }
  </code></pre>