Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi
I have two text boxes for email ids
when I give values like

in First Email : abc@abc.com
in Second Email : ABC@abc.com


It should compare both values and should display validation "both email ids should not be same"

How can I achieve this?
Posted
Updated 3-Apr-14 21:17pm
v2

use comparevalidator and set the operator property as notequal
C#
<asp:textbox id="TextBox1" runat="server"></asp:textbox>
<asp:textbox id="TextBox2" runat="server" textmode="Password" ></asp:textbox>
<asp:comparevalidator id="CompareValidator1" controltocompare="TextBox1" controltovalidate="TextBox2"  Operator="NotEqual" runat="server" 
                                ErrorMessage="CompareValidator"></asp:comparevalidator>
 
Share this answer
 
v2
XML
<script type="text/javascript" language="JavaScript">
<!--
//--------------------------------
// This code compares two fields in a form and submit it
// if they're the same, or not if they're different.
//--------------------------------
function checkEmail(theForm) {
    if (theForm.EMAIL_1.value == theForm.EMAIL_2.value)
    {
        alert('Both Emails are same,please enter another Email address!');
        return false;
    } else {
        return true;
    }
}
//-->
</script>




<form action="../" onsubmit="return checkEmail(this);">
<p> Enter Your Email Address:<br>
<input type="TEXT" name="EMAIL_1" size="20" maxlength="20">
<br>
Please Confirm Your Email Address:
<br>
<input type="TEXT" name="EMAIL_2" size="20" maxlength="20">
<br>
<input type="SUBMIT" value="Send Address!"></p>
</form>
 
Share this answer
 

Solution


Very easy. Compare by converting to lower case using JavaScript.
JavaScript
function validateEmails() {
    var firstEmail = document.getElementById('txtFirstEmail');
    var secondEmail = document.getElementById('txtSecondEmail');

    if (firstEmail != null && secondEmail != null) {
        if (firstEmail.value.toLowerCase() == secondEmail.value.toLowerCase()) {
        alert("Both email ids should not be same.");
            return false;
        }
    }

    return true;
}

Note: It does not validate for blank. If you want to do that, then please add those code yourself. I have just given the code for validating the value according to your requirements.

Demo


[Demo] Email Validation ignoring the case[^]
 
Share this answer
 
First, need to validate that they are both valid email addresses using Regex
Second, check if they are the same ignoring case, convert both to small case in the function before comparison.
See example:
XML
<head id="Head1" runat="server">
<title></title>
<script  type="text/javascript">
    function validateEmails() {
        var email1 = document.getElementById("<%=TextBox1.ClientID%>").value.toLowerCase();
        var email2 = document.getElementById("<%=TextBox2.ClientID%>").value.toLowerCase();
        if (isEmailAddress(email1) && isEmailAddress(email2) && email1 != email2) {
            alert("emails are valid and not the same.");
            return true;
        }
        else
            alert('email 1 and email 2 are either not valid or they are the same!');
        return false;
    }

   var pattern = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;

    function isEmailAddress(str) {
        return str.match(pattern);
    }

</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" OnClientClick="javascript:return validateEmails();" />
        </div>
    </form>
</body>
</html>
 
Share this answer
 
C#
function validateEmail() {
var strInput = document.getElementById('txtEmail').value;
    var pattern = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    if (!pattern.test(strInput)) {
        return false;
    }
    return true;
}


HTML
<input type="text" id="txtEmail" name="txtEmail" />
<input type="submit" value="Save"  önclick="validateEmail();" />
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900