How to create a Must Not Match attribute for email address

a 夏天 提交于 2019-12-10 23:19:32

问题


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;}

my requirement is that both email id should not be same

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

thanks,


回答1:


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;
}

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

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute%28v=vs.95%29.aspx



来源:https://stackoverflow.com/questions/10783390/how-to-create-a-must-not-match-attribute-for-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!