验证电子邮件和备用电子邮件的方法不接受相同的电子邮件?

时间:2017-07-11 07:55:24

标签: asp.net-mvc

谢谢大家..我用简单的jquery代码调用了@onblur =“checkEmail()”:

function checkEmail() {
                var mainEmail = $('#email').val();
                var altEmail = $('#altEmailTxt').val();
                if(mainEmail==altEmail)
                {
                    $("#emailvalidbox").html("Email and Alternative Email should not be Same");
                }
                else
                {
                    $("#emailvalidbox").html("");
                }
            }

1 个答案:

答案 0 :(得分:0)

您可以在这里进行自定义验证:

您需要将以下代码放在模型顶部

 [CompareTwoEmails(ErrorMessage = "Email Id Should be different")]
 public class AddressModel
 {
      public string FirstEmail { get; set; }
      public string SecondEmail { get; set; }
     //Other Properties 
 }

您的自定义验证类:

   [AttributeUsage(AttributeTargets.Class)]
   public class CompareTwoEmails : ValidationAttribute
   {
       public override bool IsValid(object value)
       {
           var model = (AddressModel)value;
           if (string.Equals(model.FirstEmail, model.SecondEmail, StringComparison.OrdinalIgnoreCase))
           {
               return false;

           }
           return true;
       }
   }

干杯!!