title: How to compare a model property for No Equality with multiple fields in Model in MVC
body: <p>I am new to MVC technology. Today i got an assignment to make a form where user can enter multiple email address on form. I need to stop user from entering duplicate email address in other text-boxes. I have created a custom validator for comparing called "NonEqualValidation" as given below</p>
  
  <pre><code> [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
  public class NonEqualValidation : ValidationAttribute, IClientValidatable
  {
      private const string DefaultErrorMessage = "Duplicate e-mail not allowed";
      public string OtherProperty { get; private set; }
      public string OtherPropertyName { get; private set; }
  
      public NonEqualValidation(string otherProperty, string otherPropertyName): base(DefaultErrorMessage)
      {
          OtherProperty = otherProperty;
          OtherPropertyName = otherPropertyName;
      }
  
      protected override ValidationResult IsValid(object value, ValidationContext validationContext)
      {
          if (value != null)
          {
              var otherProperty = validationContext.ObjectInstance.GetType().GetProperty(OtherProperty);
  
              var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);
  
              if (value.Equals(otherPropertyValue))
              {
                  return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
              }
          }
  
          return ValidationResult.Success;
  
      }
  
      public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
      {
          var rule = new ModelClientValidationRule()
          {
              ValidationType = "unlike",
              ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()),
          };
  
          rule.ValidationParameters.Add("otherproperty", OtherProperty);
          rule.ValidationParameters.Add("otherpropertyname", OtherPropertyName);
  
          yield return rule;
      }
  
      public override string FormatErrorMessage(string name)
      {
          return string.Format(ErrorMessageString, name, OtherPropertyName);
      }
  }
  </code></pre>
  
  <p>My Model structure is like below :  </p>
  
  <pre><code> [EmailAddress(ErrorMessage = "Please enter valid e-mail address")]
      [NonEqualValidation("MAILFRIEND2", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND3", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND4", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND5", "Duplicate e-mail not allowed")]
      public string MAILFRIEND1 { get; set; }
  
      [EmailAddress(ErrorMessage = "Please enter valid e-mail address")]
      [NonEqualValidation("MAILFRIEND1", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND3", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND4", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND5", "Duplicate e-mail not allowed")]
      public string MAILFRIEND2 { get; set; }
  
      [EmailAddress(ErrorMessage = "Please enter valid e-mail address")]
      [NonEqualValidation("MAILFRIEND1", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND2", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND4", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND5", "Duplicate e-mail not allowed")]
      public string MAILFRIEND3 { get; set; }
  
      [EmailAddress(ErrorMessage = "Please enter valid e-mail address")]
      [NonEqualValidation("MAILFRIEND1", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND2", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND3", "Duplicate e-mail not allowed")]
      [NonEqualValidation("MAILFRIEND5", "Duplicate e-mail not allowed")]
      public string MAILFRIEND4 { get; set; }
  
      [EmailAddress(ErrorMessage = "Please enter valid e-mail address")]
      [NonEqualValidation("MAILFRIEND1", "E-mail is use above")]
      [NonEqualValidation("MAILFRIEND2", "E-mail is use above")]
      [NonEqualValidation("MAILFRIEND3", "E-mail is use above")]
      [NonEqualValidation("MAILFRIEND4", "E-mail is use above")]
      public string MAILFRIEND5 { get; set; }
  </code></pre>
  
  <p>But still expected result is not coming. If i enter duplicate value as well in that it's not working. Can somebody help out on this.</p>
  
  <p>I have added this jquery validation as well to make it at client side like given in below post.I am not able to paste it as giving error:</p>
  
  <p><a href="http://macaalay.com/2014/02/25/unobtrusive-client-and-server-side-not-equal-to-validation-in-mvc-using-custom-data-annotations/" rel="nofollow">http://macaalay.com/2014/02/25/unobtrusive-client-and-server-side-not-equal-to-validation-in-mvc-using-custom-data-annotations/</a></p>