title: MVC 3 client side validation, model binding decimal value and culture (different decimal separator)
body: <p>I am trying to have my client side validation (model binding) to support different cultures, and I found an interesting blog on the subject on which I am trying to implement.</p>
  
  <p><a href="http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx" rel="noreferrer">http://haacked.com/archive/2011/03/19/fixing-binding-to-decimals.aspx</a></p>
  
  <p>Poco</p>
  
  <pre><code>  public class Jogador
    {
      public int ID { get; set; }
  
      public string Name { get; set; }
  
      public decimal Salary { get; set; }
    }
  </code></pre>
  
  <p>I've got my custom DecimalModelBinder class</p>
  
  <pre><code>  public class DecimalModelBinder : IModelBinder
    {
      public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
      {
        ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        ModelState modelState = new ModelState {Value = valueResult};
  
        object actualValue = null;
        try
        {
          actualValue = Convert.ToDecimal(valueResult.AttemptedValue, CultureInfo.CurrentCulture);
        }
        catch (FormatException e)
        {
          modelState.Errors.Add(e);
        }
  
        bindingContext.ModelState.Add(bindingContext.ModelName, modelState);
        return actualValue;
      }
    }
  </code></pre>
  
  <p>My web.config:</p>
  
  <p>
  </p>
  
  <p>
    
      
      
      
    </p>
  
  <p>
      </p>
  
  <pre><code>&lt;compilation debug="true" targetFramework="4.0"&gt;
    &lt;assemblies&gt;
      &lt;add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
      &lt;add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
      &lt;add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
      &lt;add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
      &lt;add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /&gt;
    &lt;/assemblies&gt;
  &lt;/compilation&gt;
  
  &lt;authentication mode="Forms"&gt;
    &lt;forms loginUrl="~/Account/LogOn" timeout="2880" /&gt;
  &lt;/authentication&gt;
  
  &lt;pages&gt;
    &lt;namespaces&gt;
      &lt;add namespace="System.Web.Helpers" /&gt;
      &lt;add namespace="System.Web.Mvc" /&gt;
      &lt;add namespace="System.Web.Mvc.Ajax" /&gt;
      &lt;add namespace="System.Web.Mvc.Html" /&gt;
      &lt;add namespace="System.Web.Routing" /&gt;
      &lt;add namespace="System.Web.WebPages"/&gt;
    &lt;/namespaces&gt;
  &lt;/pages&gt;
  </code></pre>
  
  <p></p>
  
  <p>
      
      
    </p>
  
  <p>
      
        
          
          
        
      
    
  </p>
  
  <p>Global.asax are altered to use my custom ModelBinder on decimal and decimal? values</p>
  
  <pre><code>protected void Application_Start()
  {
    AreaRegistration.RegisterAllAreas();
  
    ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder());
    ModelBinders.Binders.Add(typeof(decimal?), new DecimalModelBinder());
  
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>
  
  <p>Still the client-side validation fails on decimal entered in my view with a "," as a decimal separator. It does not handle both "," and ".". The js validation does not seem to take my custom binding in consideration</p>
  
  <p>Reading the blog article over and over again, I just can't seem to figure out what I am missing.</p>
  
  <p>Here is the view:</p>
  
  <pre><code>@model MVC_Empty.Web.Models.Jogador
  
  @{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"&gt;&lt;/script&gt;
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;Jogador&lt;/legend&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Name)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Name)
              @Html.ValidationMessageFor(model =&gt; model.Name)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Salary)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Salary)
              @Html.ValidationMessageFor(model =&gt; model.Salary)
          &lt;/div&gt;
  
          &lt;p&gt;
              &lt;input type="submit" value="Create" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>
  
  <p>Server side validation seems fine, but how to handle the client-side validation in order to send a POST when clicking the submit button.</p>
  
  <p>The javascript validation  does not handle the comma.<img src="https://i.stack.imgur.com/wVpyh.png" alt="enter image description here">
  <img src="https://i.stack.imgur.com/sA07n.png" alt="enter image description here"></p>