0

Thank you Guys.. I succeeded it with simple jquery code with calling @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("");
                }
            }
Luqman
  • 139
  • 2
  • 15
  • You need to create custom data annotation for that check this link for that : http://www.c-sharpcorner.com/UploadFile/rahul4_saxena/mvc-4-custom-validation-data-annotation-attribute/ – Laxman Gite Jul 11 '17 at 08:16
  • The only real way to validate an email address is to send an email to it and confirm that it was received. – Luke Jul 11 '17 at 08:23
  • Seems that what you want is creating custom attribute like there: https://stackoverflow.com/questions/8786251/opposite-of-compare-data-annotation-in-net. – Tetsuya Yamamoto Jul 11 '17 at 09:01
  • @LaxmanGite : Is there any example with you for this problem. the exmple you provided is for validating email for correct format. – Luqman Jul 11 '17 at 09:10
  • you can use compare attribute for this. Assuming your primary email id field as public string PrimaryEmail { get; set; } [Compare(CompareField = PrimaryEmail)] public string SecondaryEmail { get; set; } – Amit Mishra Jul 11 '17 at 09:17
  • @AmitMishra : it will be validating for equal conditions only, but here i need not equal condition. – Luqman Jul 11 '17 at 09:47
  • Thank u anyways.. I got the problem resolved by the above answer which i found my own method – Luqman Jul 14 '17 at 06:52

1 Answers1

0

Here you go for the custom validation:

You need to place below line code on the top of your model

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

And your custom validation class :

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

Cheers !!

Laxman Gite
  • 2,248
  • 2
  • 15
  • 23
  • Thank u anyways.. I got the problem resolved by the above answer which i found my own methos. – Luqman Jul 11 '17 at 10:46