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!

Desparate Help Needed

Status
Not open for further replies.

subminimal

Programmer
Jan 11, 2004
7
US
I have a javascript that takes two text boxes, compares the numbers, and outputs the similarities to a third textbox. For example, if textbox one is 1234, and textbox two is 123, the output would be 123, as the 1,2, and 3 are the only numbers in the two that are the same. here is the code:

Code:
<script language="JavaScript" type="text/javascript">
<!--

function Compare(){
    var common="";
    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>
<table width="560" border="1" align="center">
  <tr>
    <td width="566"><form onsubmit="return Compare();">
      First number:
        <p>
        <input name="num1" type="text">
        <br>
    Second number:</p>
      <p>
        <input name="num2" type="text">
        <br>
    Similarities:</p>
      <p>
        <input name="result" type="text">
        <br>
      </p>
      <p>
        <input name="submit" type="submit" value="Calculate">
      </p>
    </form>
    </td>
  </tr>
</table>
I need to changenge the code, so it will dynamically generate the number of boxes, for example, between 1 and 20 boxes,(as in a message will ask "how many boxes?") then the user can input numbers into those boxes and output the results the same way, only outputting the numbers that are the same in all the boxes. Is this possible? Any help would be GREATLY appreciated, as I am not very well versed in javascript. Thankyou in advance for any help you can provide.
 
Try this. I haven't tested it fully... don't know if it'll work....just a thought

<html>
<head>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<script language="JavaScript">
function CreateBox()
{
var str='';
for(i=0;i<document.Form1.texvalue.value;i++)
{
str = str + '<input type=text name=num'+i+'><br>';
}

document.getElementById("layer").innerHTML = str;


}
function Compare()
{
for(i=0;i<document.Form1.elements.length;i++)
{
var elt = document.Form1.elements.name;

if(elt.indexOf("num") > -1)
{
if(elt == "num0")
{
var prevelt = document.Form1.elements.value;
}
else
{
var str = document.Form1.elements.value;
if(str.length > prevelt.length)
{
var prevelt = str.match(prevelt);
}
else
{
var prevelt = prevelt.match(str);
}

}
}

}
alert("final "+prevelt);
document.Form1.result.value = prevelt;

}
</script>

</head>
<body>


<form name="Form1" ID="Form1" onsubmit="return Compare();">
<input type=text name="texvalue" value=""><br>
<input type=button value="Submit" onclick="CreateBox();"><br><br>
<div id="Layer" >
</div>
<div id="Layer2">
Similarities:<br>
<input type=text name="result"><br>
<p>
<input name="submit" type="submit" value="Calculate" ID="Submit1">
</p>

</div>
</form>



</body>
</html>




rsshetty.

It's always in the details.
 
There's some problem with prevelt.length.

Maybe you can fiddle around in the meantime.

rsshetty.

It's always in the details.
 

2 questions:

1. How does this question and response differ from the other thread you started last month, which, according to yourself, seemed to answer your question perfectly: thread216-746717

2. What is this going to be used for? It always helps to have some idea of the use for a script.

Dan
 
rsshetty, thank you very much for your help. I'll fiddle with it some more, just tried the script and it seems to work on smaller numbers, i need to be able to input up to 20, but its an excellent starting point. Thanks again.

BillyRayPreachersSon, the last thread was answered perfectly, it worked exactly as my friend needed. Unfortunetley, she decided she needed a dynamic textbox set so she wouldnt be limited to 2 or 3. As for the purpose, she wont tell me that until whatever shes using it for actually happens, lol, not the best way to work I'm sure, but shes always been a bit odd. For all your help the least I can do is post exactly what this odd little script is for as soon as I find out myself. Thanks again, all, for your your help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top