Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2009, 2:51 PM   #1
organizedchaos
Programmer
 
Join Date: Oct 2008
Posts: 35
Rep Power: 0 organizedchaos is on a distinguished road
Compare Input from Array Values

Hi! How do I compare user input from the values in my array and get its index?
organizedchaos is offline   Reply With Quote
Old Feb 9th, 2009, 5:40 PM   #2
Arla
Expert Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 778
Rep Power: 6 Arla is on a distinguished road
Re: Compare Input from Array Values

Just loop through the Array from 0 to N (where N is the size of the array) and when you find a match, you know the index.

At least that's a way to do it (don't know Javascript so can't say explicitly how to do it in Javascript)
__________________
I can remember, back in '22
They changed the law - came knocking on the door
In that same moment, the broadband seemed to go..
Phones all dead. Gone dizzy in the head..
Arla is offline   Reply With Quote
Old Feb 10th, 2009, 1:17 PM   #3
organizedchaos
Programmer
 
Join Date: Oct 2008
Posts: 35
Rep Power: 0 organizedchaos is on a distinguished road
Re: Compare Input from Array Values

wow, thanks!
organizedchaos is offline   Reply With Quote
Old May 27th, 2009, 1:26 PM   #4
csrocker101
Hobbyist Programmer
 
Join Date: Dec 2006
Posts: 119
Rep Power: 4 csrocker101 is on a distinguished road
Re: Compare Input from Array Values

This is pretty sloppy but you'll get the general idea....Something like:

function searchArray()
{
    var array = new Array(1,2,3,4,5,6);
    
   //matchValue would probably come from a field input
    var matchValue = 3;
    var i;    

    for(i = 0; i < array.length; i++)
    {
          if(matchValue == array[i])
          {
              //exit the loop or call another method
              matchExists();
          }
     } 
}
function matchExists()
{
        //A match exists!!!
}
csrocker101 is offline   Reply With Quote
Old May 27th, 2009, 3:08 PM   #5
csrocker101
Hobbyist Programmer
 
Join Date: Dec 2006
Posts: 119
Rep Power: 4 csrocker101 is on a distinguished road
Re: Compare Input from Array Values

If you want the index:

var i

Would be the index at which the value is located.
csrocker101 is offline   Reply With Quote
Old May 29th, 2009, 5:40 AM   #6
essential
Newbie
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0 essential is on a distinguished road
Re: Compare Input from Array Values

Here's another DEMO of comparing userInput against your array lists.
javascript Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html id="xhtml10" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <head>
  6. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  7. <meta http-equiv="Content-Script-Type" content="text/javascript" />
  8. <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
  9. <title>DANIuser :: essential</title>
  10. <style type="text/css">
  11. /* <![CDATA[ */
  12. html, body {
  13. background-color : #ccc;
  14. color : #405060;
  15. font : normal normal normal 95%/1.4 "Trebuchet MS", "Bernard MT Condensed", Tahoma, Verdana, Helvetica, Arial, sans-serif;
  16. min-height : 600px;
  17. text-align : center;
  18. height : auto;
  19. width : auto; }
  20.  
  21. body .m-title {
  22. background-color : #444;
  23. border-top : 2px solid #000;
  24. border-bottom : 2px solid #000;
  25. color : #757575;
  26. margin-top : 0;
  27. padding : .100em 1em .100em 1em;
  28. text-align : left;
  29. width : auto; }
  30.  
  31. div#main {
  32. background-color : #f0f0f0;
  33. margin : 0 auto;
  34. height : inherit !important;
  35. padding : 0;
  36. width : 100%; }
  37.  
  38. div.tube {
  39. border : 1px solid;
  40. background-color : inherit;
  41. height : inherit !important;
  42. clear : both;
  43. padding : 1em;
  44. margin : 0;
  45. overflow : hidden; }
  46.  
  47. iframe {
  48. display : block;
  49. border : 1px solid #405060;
  50. min-height : 600px;
  51. width : 100%; }
  52.  
  53. div#demo {
  54. background-color : #ccc;
  55. margin-top : 2em;
  56. min-height : 30px;
  57. padding : 0 1em 0 1em;
  58. white-space : nowrap; }
  59. /* ]]> */
  60. </style>
  61. <script type="text/javascript">
  62. // <![CDATA[
  63. var uInput;
  64. var makeMatch = [ "Blue", "Red", "Green", "Yellow" ];
  65. var myArray = [ ];
  66. var check = function() {
  67. uInput = (( document.getElementById ) ? document.getElementById("txt") : (( document.all ) ? document.all.txt : document.layers.txt )).value;
  68. for ( var x = 0; x < makeMatch.length; x++ ) {
  69. myArray[ makeMatch[ x ] ] = makeMatch[ x ];
  70. }
  71. if ( myArray[ uInput ] ) {
  72. alert("Matched found: \u0022" + myArray[ uInput ] + "\u0022");
  73. } else {
  74. alert("Sorry, no match found!");
  75. }
  76. };
  77. // ]]>
  78. </script>
  79. </head>
  80. <body>
  81. <div id="main">
  82. <div id="content" class="tube">
  83. <h2 class="m-title">Javascript Live Help!</h2>
  84. <div id="demo">
  85. <input type="text" id="txt" name="txt" value="" /> <button id="btn" name="btn" onclick="check();">Try Match</button></div>
  86. </div>
  87. </div>
  88. </body>
  89. </html>
hope you find it useful...
essential is offline   Reply With Quote
Old Jun 5th, 2009, 4:07 PM   #7
organizedchaos
Programmer
 
Join Date: Oct 2008
Posts: 35
Rep Power: 0 organizedchaos is on a distinguished road
Re: Compare Input from Array Values

Thanks! That's really helpful.
organizedchaos is offline   Reply With Quote
Old Feb 4th, 2010, 10:04 PM   #8
kn0wl3dg3
Information Systems *****
 
kn0wl3dg3's Avatar
 
Join Date: Feb 2010
Location: Twin Cities
Posts: 29
Rep Power: 0 kn0wl3dg3 is an unknown quantity at this point
Send a message via Skype™ to kn0wl3dg3
Re: Compare Input from Array Values

essential,
Great post!
One Question...
How could you get that to work when the user hits return also, instead of just onclick?
kn0wl3dg3 is offline   Reply With Quote
Old Feb 6th, 2010, 9:56 AM   #9
essential
Newbie
 
Join Date: Apr 2009
Posts: 6
Rep Power: 0 essential is on a distinguished road
Re: Compare Input from Array Values

kn0wl3g3,

I would be willing to help you if you can provide me with a more specific question...

essential
essential is offline   Reply With Quote
Old Feb 6th, 2010, 11:42 AM   #10
kn0wl3dg3
Information Systems *****
 
kn0wl3dg3's Avatar
 
Join Date: Feb 2010
Location: Twin Cities
Posts: 29
Rep Power: 0 kn0wl3dg3 is an unknown quantity at this point
Send a message via Skype™ to kn0wl3dg3
Re: Compare Input from Array Values

Quote:
Originally Posted by kn0wl3dg3 View Post
essential,
Great post!
One Question...
How could you get that to work when the user hits return also, instead of just onclick?
Well, I was playing with the code from your earlier post, and got it to read from an array and direct to a page if a matching .html file is found...
What my question was referring to was that it only works when the user clicks the button with the mouse, instead of hitting return on the keyboard. I found a fix using an onsubmit event also, but was wondering if you had a different solution, since it needs to have the call in two places to do it this way
kn0wl3dg3 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting char values from an array using pointers IceCold19 C 2 Jan 6th, 2009 4:29 PM
default values in input box not visible sk123 HTML / XHTML / CSS 4 Oct 21st, 2008 7:39 PM
Reading character input into an array (raw mode) shoeyfighter C 3 Nov 2nd, 2006 3:49 PM
Combining Values in an array bigmitch C++ 4 Feb 10th, 2006 7:29 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 2:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC