title: How to change MVC custom client-side validation error message at runtime using jQuery?
body: <p>I am using MVC5, and I have a ViewModel for my View, which contains a simple form with the following fields:</p>
  
  <pre><code>MinFirstNameLength
  FirstName
  MinLastNameLength
  LastName
  </code></pre>
  
  <p>Now, I wish to apply a validation rule on <code>FirstName</code>, based on the value of <code>MinFirstNameLength</code>, and similarly for <code>LastName</code> using <code>MinLastNameLength</code>. I want to do this on the client-side too.</p>
  
  <p>So, I used MVC's unobtrusive client side validation feature. I made a custom validation attribute, implementing the <code>IClientValidatable</code> interface. The <code>GetClientValidationRules</code> method looks like this:</p>
  
  <pre><code>public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
  {
      string ErrorMessage = ErrorMessageString;
      ModelClientValidationRule NameMinLengthRule = new ModelClientValidationRule();
      NameMinLengthRule.ErrorMessage = ErrorMessage;
      NameMinLengthRule.ValidationType = "nameminlength";
      NameMinLengthRule.ValidationParameters.Add("minlengthpropname", MinLengthPropName);
      yield return NameMinLengthRule;
  }
  </code></pre>
  
  <p>This validation attribute is applied on the <code>FirstName</code> and <code>LastName</code> properties like this:</p>
  
  <pre><code>[NameMinLength("FirstNameMinLength",ErrorMessage = "First Name must be at least {0} characters"]
  public string FirstName { get; set; }
  
  [NameMinLength("LastNameMinLength",ErrorMessage = "Last Name must be at least {0} characters"]
  public string LastName { get; set; }
  </code></pre>
  
  <p>Also, I have the client side validation functions in a .js file elsewhere, which looks like this:</p>
  
  <pre><code>$.validator.addMethod("nameminlength",
  function (
      value,
      element,
      params
  ) {
      return value.length &gt;= parseInt($(params).val());
  });
  
  $.validator.unobtrusive.adapters.add("nameminlength", ["minlengthpropname"], function (options) {
      var paramField = "#" + options.params.minlengthpropname;
      options.rules["nameminlength"] = paramField;
      var errormessage = options.message;
      options.messages["nameminlength"] = errormessage;
  });
  </code></pre>
  
  <p>My question is, how do I assign the value in the textbox for <code>MinFirstNameLength</code> to the placeholder <code>{0}</code> in my error message for <code>FirstName</code>, so that the error message reads <code>First Name must be at least [value] characters</code>?</p>
  
  <p>I tried adding modifying my <code>$.validator.unobtrusive.adapters.add</code> method as follows:</p>
  
  <pre><code>$.validator.unobtrusive.adapters.add("nameminlength", ["minlengthpropname"], function (options) {
      var paramField = "#" + options.params.minlengthpropname;
      options.rules["nameminlength"] = paramField;
      var errormessage = options.message;
      options.messages["nameminlength"] = errormessage;
      $(paramField).blur(function () {
          // change error message in the 'data-val-nameminlength' attribute of the HTML element on which validation is applied
          var newErrorMessage = options.messages["nameminlength"].replace("{0}", $(paramField).val());
          $(options.element).attr("data-val-nameminlength", newErrorMessage);
          // change error message inside the error message span generated for displaying this error
          $(options.element).siblings(".field-validation-valid").html(newErrorMessage);
      });
  });
  </code></pre>
  
  <p>But both these tricks didn't work. The HTML markup did change to modify the error appropriately, but this change wasn't reflected in the page.</p>
  
  <p>Currently, the error message I see is:</p>
  
  <p><code>First Name must be at least #FirstNameMinLength characters</code></p>
  
  <p>The placeholder is replaced automatically by the id of the control which is used as a parameter in the validation rule.</p>
  
  <p>Where does this error message come from? How do I modify it at runtime?</p>