Reputation: 4270
I have one question related to Javascript Validation:
How could I find the difference(the characters which are available in first label but not are in second label) between two Labels ?
Thanks - see the code below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<table width="50%" border="0" cellspacing="0" cellpadding="4" style="font-family:Arial, Helvetica, sans-serif; font-size:12px;">
<tr>
<td width="44%" align="right">Label 1</td>
<td width="56%"><input type="text" value="ABCD" /></td>
<td width="56%" style="color:#C00;">Note</td>
</tr>
<tr>
<td align="right">Label 2</td>
<td><input type="text" value="AEBHL" /></td>
<td rowspan="2" valign="top">I want to show the difference of Label 1 and Label 2 in Diffrence's input. How could I find the difference?</td>
</tr>
<tr>
<td align="right">Difference</td>
<td><input type="text" value="EHL" style="border:1px solid #C00;" /></td>
</tr>
</table>
</body>
</html>
Upvotes: 0
Views: 187
Reputation: 10180
Get the text of the first label.
Get the text of the second label.
Compute the difference between the two labels. Assign the difference to the difference row.
You can see it working here: http://jsfiddle.net/Anton87/RDvJf/
var label1 = jQuery("table tr:eq(0) input").val(),
label2 = jQuery("table tr:eq(1) input").val(),
diff2 = difference(label2, label1);
jQuery("table tr:eq(2) input").val(diff2);
function difference(label1, label2) {
var difference = [];
for (var i = 0, max = label1.length; i < max; i++) {
if (label2.indexOf(label1[i]) === -1) {
difference.push(label1[i]);
}
}
return difference;
}
Upvotes: 2