Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript Help please 1

Status
Not open for further replies.

subminimal

Programmer
Jan 11, 2004
7
US
I need to write a script or program that will compare two 12-16 digit numbers and then output thier similarites to a textbox (so they can be copied/pasted). I envision two textboxes where you would type in each number, a butto to trigger, and an output text box. For example, 112458729001 and 528771902635 would yeild 12587290 as the output. I apologize for my newbie description here, but any help would be greatly appreciated.
 
Try the following code... I could only test with your examples, so you might want to run it through a lot more. :)

Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;>
<html>
<head>
	<title>Finding...</title>
<script language=&quot;JavaScript&quot; type=&quot;text/javascript&quot;>
<!--
function Compare(){
	var common=&quot;&quot;;
	var num1=document.forms[0].num1.value;
	var num2=document.forms[0].num2.value;
	var res=document.forms[0].result;
	var used=new Array(num2.length);
	for(x=0;x<num2.length;x++){
		used[x]=0;
	}
	for(x=0;x<num1.length;x++){
		var toFind=num1.charAt(x);
		for(y=0;y<num2.length;y++){
			if(toFind==num2.charAt(y)){
				// found digit in second string - has it been used as a match before?
				if(used[y]==0){
					// not matched yet... so it's a match
					common+=num1.charAt(x);
					used[y]=1;
					y=num2.length;
				} else {
					// already used
				}
			}
		}
	}
	res.value=common;
	return false;
}
//-->
</script>
</head>
<body>
<form onsubmit=&quot;return Compare();&quot;>
 First number: <input name=&quot;num1&quot; type=&quot;text&quot;><br>
Second number: <input name=&quot;num2&quot; type=&quot;text&quot;><br>
RESULT: <input name=&quot;result&quot; type=&quot;text&quot;><br>
<input type=&quot;submit&quot; value=&quot;Test&quot;>
</form>
</body>
</html>

Hope it helps.

Pete.


Lotus Notes Web Developer / Aptrix (LWWCM) Consultant
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Thank you so very very much. It works perfectly and I learned something in the process. Again, my sincerest thanks.

Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top