title: File upload field validation in Web Forms for Marketers 8.1 update 3
body: <p>I am trying file upload validation in sitecore 8 using Web forms for Marketers but it is not working please suggest me solution. Here is my code.</p>
  
  
  
  <pre class="lang-cs prettyprint-override"><code>namespace Test.Web.WFFM
  {
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
    public class LimitFileSizeAttribute : DynamicValidationBase
    {
  
      private const string FileSizeLimitKey = "filesizelimit";
      private const string ErrorMessageKey = "filesizelimiterror";
  
      protected override ValidationResult ValidateFieldValue(IViewModel model, object value, ValidationContext validationContext)
      {
  
          // Required attribute should handle null value
          if (value == null)
          {
              return ValidationResult.Success;
          }
  
          var fileBase = value as HttpPostedFileBase;
          if (fileBase == null)
          {
              return new ValidationResult(ErrorMessage);
          }
  
          var fileUploadField = validationContext.ObjectInstance as FileUploadField;
          var limit = GetLimit(fileUploadField);
  
          if (limit == -1) // Limit not set
          {
              return ValidationResult.Success;
          }
  
          if (fileBase.ContentLength &gt; limit)
          {
              return new ValidationResult(GetErrorMessage(fileUploadField));
          }
  
          return ValidationResult.Success;
      }
  
      private static int GetLimit(IViewModel field)
      {
          if (field == null || !field.Parameters.ContainsKey(FileSizeLimitKey))
          {
              return -1;
          }
  
          var parameter = field.Parameters[FileSizeLimitKey];
          int intValue;
          if (int.TryParse(parameter, out intValue))
          {
              return intValue;
          }
  
          return -1;
      }
  
      private string GetErrorMessage(IViewModel field)
      {
          if (field != null &amp;&amp; field.Parameters.ContainsKey(ErrorMessageKey))
          {
              return field.Parameters[ErrorMessageKey];
          }
  
          return ErrorMessage;
      }
  }}
  </code></pre>
  
  <blockquote>
    <p>Created RestrictedUploadField class</p>
  </blockquote>
  
  <pre class="lang-cs prettyprint-override"><code>namespace Test.Web.WFFM.MVC
  {
    public class RestrictedUploadField : FileUploadField
    { 
      [LimitFileSize]
      public override HttpPostedFileBase Value { get; set; }
  }}
  </code></pre>
  
  <blockquote>
    <p>Created custom validation in sitecore and added "Parameters" and "Localized Parameters"</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/34vv4.png" rel="nofollow"><img src="https://i.stack.imgur.com/34vv4.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Cz6wX.png" rel="nofollow"><img src="https://i.stack.imgur.com/Cz6wX.png" alt="enter image description here"></a></p>
  
  <p>Created custom field type "Restricted File Upload", added validation in "Validation" field, added "Parameters" and "Localized Parameters".
  <a href="https://i.stack.imgur.com/0rJwp.png" rel="nofollow"><img src="https://i.stack.imgur.com/0rJwp.png" alt="enter image description here"></a></p>