Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Have Two Multiline Textboxes and in first textbox i enter some numbers with duplicates also and now i enter numbers in second textbox.when i press filter button i get the values which are available in both the textboxes like
textbox1
9900990099,9977889977,9900887766
textbox2
9900990099,1234567890,0987654321

whn i press filter button i get
9900990099 ( both the textboxes are have)
how can i do this in Javascript. pls help me
Posted
Comments
Sergey Alexandrovich Kryukov 24-Jan-15 12:48pm    
Wouldn't that be better to avoid duplicates in first place?
—SA

1 solution

Try the below code. It works for ASP.NET; from your comment, I saw you used that.
JavaScript
var txt1 = document.getElementById('<%= txt1.ClientID %>').value;
var txt2 = document.getElementById('<%= txt2.ClientID %>').value;
var txt1values = txt1.split(",");
var txt2values = txt2.split(",");
var commonValues = [];
for (var i = 0; i < txt1values.length; i++) {
    var curr = txt1values[i];
    if (txt2values.indexOf(curr) != -1) {
        commonValues.push(curr);
    }
}
var commonValuesStr = commonValues.join(",");
document.getElementById('<%= txt3.ClientID %>').value = commonValuesStr;

How does this work? The ASP.NET server replaces <%= txt1.ClientID %> by the ID that can be used on the client side. Then, the code splits the text by a comma. Then, it loops over all values of the first textbox. Is that value also in the values of the second textbox? Then it gets added to commonValues. At the end, all values in commonValues get joined with a comma.
 
Share this answer
 
v4
Comments
benzimen 24-Jan-15 7:50am    
thanks for u r reply..i have one doubt what it is in javascript we get any control value with the help of document.getelementbtid("txt1").value;
but you are not mentioned...
Thomas Daniels 24-Jan-15 7:54am    
Oh, right, I forgot to mention that. Is your textbox a input type="text" or a textarea?
benzimen 24-Jan-15 7:55am    
<asp:TextBox ID="txt1" TextMode="MultiLine" runat="server">
Thomas Daniels 24-Jan-15 8:06am    
I edited my answer.
benzimen 24-Jan-15 8:09am    
intelligance does not show split()
var txt1values = txt1.

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