title: ASP.NET MVC client-side validation of custom attribute
body: <p>How to fire a custom validator in client side?
  This is what i have till now:</p>
  
  <p>My validation class:</p>
  
  <pre><code>public class AlmostEqual : ValidationAttribute, IClientValidatable
  {
      private readonly string _otherProperty;
      private readonly float _myPercent;
      public AlmostEqual(string otherProperty,float percent)
      {
          _otherProperty = otherProperty;
          _myPercent = percent;
      }
  
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          var property = validationContext.ObjectType.GetProperty(_otherProperty);
  
          var otherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);
  
          dbEntities db = new dbEntities();
          Metal metal = db.Metals.Find(Convert.ToInt32(otherPropertyValue));
  
          double _unitWeight = metal.UnitWeight;
          double _percent = metal.UnitWeight * (_myPercent / 100);
  
          double myProperty = double.Parse(value.ToString());
  
          bool result = myProperty &gt;= _unitWeight - _percent &amp;&amp; myProperty &lt;= _unitWeight + _percent;
  
          if (!result)
          {
               return new ValidationResult(string.Format(
                      CultureInfo.CurrentCulture,
                      FormatErrorMessage(validationContext.DisplayName),
                      new[] { _otherProperty }
                  ));
          }
  
  
          return null;
      }
  
  
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          var rule = new ModelClientValidationRule
          {
              ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
              ValidationType = "almostequal",
          };
          rule.ValidationParameters.Add("other", _otherProperty);
          yield return rule;
      }
  
  
  }
  </code></pre>
  
  <p>Code from Metdata class:</p>
  
  <pre><code>        [Required]           
          [AlmostEqual("IDMetal",5,ErrorMessage="Weight do not correspond with dimensions.")]
          public Nullable&lt;double&gt; UnitWeight { get; set; }
      }
  </code></pre>
  
  <p>In view I added this js:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
        $.validator.unobtrusive.adapters.addBool("almostequal", "Range");
  &lt;/script&gt;
  </code></pre>
  
  <p>My webconfig contains:</p>
  
  <pre><code> &lt;add key="ClientValidationEnabled" value="true" /&gt;
   &lt;add key="UnobtrusiveJavaScriptEnabled" value="true" /&gt;
  </code></pre>
  
  <p>I get the error:</p>
  
  <p><em>Uncaught TypeError: Cannot read property 'call' of undefined in file
  jquery.validate.min.js at line 27</em></p>