471,892 Members | 2,095 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,892 software developers and data experts.

How to lookup/compare a value in a table..

57
I have a table in a HTML form which has multiple rows, each row contains a user ID (too many to list on this forum).. The form has an input field at the top which the user types in a user ID, I want the form to compare this to the table. If there is a match to return true, if there is no match to return cancel.

I would user an onchange to call the javascript function.

Hope that's enough to go on, I shoul be able to modify any type of example to work... Basically though, it need to compare one field to the rows in the table and find a match.

Any suggestions greatly appreciated..
Sep 10 '07 #1
6 1684
acoder
16,027 Expert Mod 8TB
See this example.
Sep 10 '07 #2
iam_clint
1,208 Expert 1GB
or you can try to modify this code i made for reading tables like excel

http://www.thescripts.com/forum/thread646585.html
Sep 10 '07 #3
plumba
57
or you can try to modify this code i made for reading tables like excel

http://www.thescripts.com/forum/thread646585.html
Thanks for the suggestions but I'm still getting no joy.

I've now modified the page slightly, all the values are in a select box called 'userid'. The user then puts in an ID in a text box called '1stDefinition''. The script should then se if the text entered in the 'existingid' field matches up to of the options in the 'userid' select box - regardless of which option is selected - it should make no difference as the select box will be hidden and is used only for this check.

The script I have so far is:

Expand|Select|Wrap|Line Numbers
  1. function compareID(){
  2.  
  3. var idlist = document.getElementById('1stDefinition').value;
  4. var idinput = document.getElementById('existingid');
  5.  
  6. alert(idlist);
  7. alert(idinput.value);
  8.  
  9. if (idlist == idinput.value){
  10.     alert("Match");
  11.     }else{
  12.     alert("No match");
  13.     }
  14.  
  15.  
  16. }
So you can see it only looks up the selected option in the '1stDefinition' select box - it should step through the whole lot to see if there is a match.

Regards
Sep 11 '07 #4
iam_clint
1,208 Expert 1GB
I think this is more what you was looking for

it loops through the options and compairs each value.
Expand|Select|Wrap|Line Numbers
  1. function compareID() {
  2. var idlist = document.getElementById('1stDefinition').options;
  3. var idinput = document.getElementById('existingid');
  4. var match = false;
  5. for (i=0; i<idlist.length; i++) {
  6.   if (idlist[i].value.toLowerCase() == idinput.value.toLowerCase()){ match = true;  }
  7. if (match) { alert("match"); } else { alert("no match"); }
  8. }
  9.  
Sep 11 '07 #5
plumba
57
I think this is more what you was looking for

it loops through the options and compairs each value.
Expand|Select|Wrap|Line Numbers
  1. function compareID() {
  2. var idlist = document.getElementById('1stDefinition').options;
  3. var idinput = document.getElementById('existingid');
  4. var match = false;
  5. for (i=0; i<idlist.length; i++) {
  6.   if (idlist[i].value.toLowerCase() == idinput.value.toLowerCase()){ match = true;  }
  7. if (match) { alert("match"); } else { alert("no match"); }
  8. }
  9.  
Hi, many thanks for that, the code seems to make sense but IE is throwing back an error saying that on a line in the form an object is expected, this line:

[HTML]<input type="button" onClick="compareID();" value="Compare">[/HTML]

That is the line which contains the button which calls the function and nothing else........ any sugestions??
Sep 11 '07 #6
iam_clint
1,208 Expert 1GB
Well the code i gave you works
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function compareID() {
  3. var idlist = document.getElementById('1stDefinition').options;
  4. var idinput = document.getElementById('existingid');
  5. var match = false;
  6. for (i=0; i<idlist.length; i++) {
  7.   if (idlist[i].value.toLowerCase() == idinput.value.toLowerCase()){ match = true;  }
  8. if (match) { alert("match"); } else { alert("no match"); }
  9. }
  10. </script>
  11. <input type="text" onblur="compareID();" value="" id="existingid"><select id="1stDefinition" name="1stDefinition"><option value="test">test</option><option value="apple">apple</option><option value="orange">orange</option></select>
  12.  
  13.  
theres my test page for it


So something your doing or something has changed.
Sep 11 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.