-2

Can someone help me out with validation of two text-box with same email Id. I was able to pop an alert if both the text-box contain the same email Id via JavaScript(my requirement was both text-box cant have same email) but now I m facing a problem if second text box contain more then one email_Id separated my comma(,) the validation doesn't work. I don't want email that is present in first text box repeat into second text-box.

My code:

<script language="javascript" type="text/javascript"> 
    function validated() { 
        if (document.getElementById("<%=txtCountry.ClientID %>").value = document.getElementById("<%=txtnewViewer.ClientID %>").value) { 
            alert("Presenter cant be attende"); 
            return false; 
        }Else{ 
            return true;
        } 
     } 
</script>
Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
Dhaval
  • 17
  • 2
  • 7
  • 1
    Have you a sample of code ? How are stored the emails ? In an object ? Just in the box ? – Sacreyoule Jul 07 '15 at 07:25
  • – Dhaval Jul 07 '15 at 07:25
  • email are in the box – Dhaval Jul 07 '15 at 07:26
  • Get the value of the second input field, split it at the comma to get an array of individual “email ids”, and then search that array for whether it contains the value of the first input field or not. – CBroe Jul 07 '15 at 07:31

3 Answers3

1

check this code out

 <script language="javascript" type="text/javascript">
 function validated()
 { 
    if (document.getElementById("<%=textbox1.id %>").value == document.getElementById("<%=textbox2.id %>").value) 
    {
      alert("text-box cant have same email"); 
      return false; 
    }
    else
    {
      alert("Valid");
      return true;
    }
 } 
</script>
0

Can you try this.

var f_email = document.getElementById("f_email").value;
var s_email= document.getElementById("s_email").value;

if(f_email === s_email) {
    // do something when email ids are same.
    alert("email ids are same");
}
else {
    // do something when email ids are same.
    alert("email ids are not same");
}
SK.
  • 4,174
  • 4
  • 30
  • 48
0

First, you if statement contains an = who always return true and modify your variable (in place of ==).

function validated() {
    var clientId = document.getElementById("<%=txtCountry.ClientID %>").value,
        viewerId = document.getElementById("<%=txtnewViewer.ClientID %>").value;
    if (clientId == viewerId) {
        alert("Presenter cant be attende"); 
        return false; 
    } 
    return true; 
}

After that you can use : Array.indexOf():

var clients = clientId.split(","), viewers = viewerId.split(",");
// Here we have two arrays with all datas
for(var i = 0; i < clients.length; i++){
    var k = viewers.indexOf(clients[i]);
    if(k !== -1) {
         alert(clients[i], "=", viewers[k]);
    }
}
Sacreyoule
  • 180
  • 9