如何创建电子邮件地址必须不匹配属性 [英] How to create a Must Not Match attribute for email address

查看:285
本文介绍了如何创建电子邮件地址必须不匹配属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的数据注解来验证我特性,以下是我的网页属性。

i am using data annotation to validate my properties, below are the properties for my page

public string YourEmail{get;set;}
public string AnotherEmail{get;set;}

我的要求是,无论电子邮件ID不应该是一样的。

my requirement is that both email id should not be same

请建议我应该从数据标注使用来解决这个问题。

please suggest what should i use from data annotation to solve this issue

感谢,

推荐答案

您必须实现IValidatableObject界面模型和模型中的添加方法验证。

You have to implement IValidatableObject interface in your model and add method Validate in your model.

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
      List<ValidationResult> validationResults = new List<ValidationResult>();

      if (string.Equals(this.Email1,this.Email2,StringComparison.OrdinalIgnoreCase))
      {
          validationResults.Add(new ValidationResult(ErrorMessage.EmailError, new string[] { "Email ID" }));
      }

      return validationResults;
}

也可以创建自定义dataannotation按照您的要求。请参考以下网址。

Or You can create the custom dataannotation as per your requirement. Please refer below URL.

<一个href=\"http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28v=vs.95%29.aspx\" rel=\"nofollow\">http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28v=vs.95%29.aspx

这篇关于如何创建电子邮件地址必须不匹配属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆