#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    31
    Rep Power
    2

    Alert when not found a value in array


    Hello i'm trying to display an alert tab when not found a value in an array in javascript. My code is this
    Code:
    var Autocomplete = [
        'an apple','alligator','elephant','pear','kingbird',
        'kingbolt','kingcraft','kingcup','kingdom','kingfisher',
        'kingpin','SML'
    ];
    
    
    \$('#ACOMP').autocomplete({
        source: Autocomplete
    });
    
    
    
    function kontrollo(Autocomplete,ACOMP) { 
    
        for (var i=0; i<Autocomplete.length; i++){
            if (Autocomplete[i] ===  ACOMP){
                 alert("ok!!!!!!");
            }  
            else{
            alert ("error"); 
            }  
        }   
    }
    ACOMP is the id that get from the form in html.
    what it does is displays "error" "error" ... when first run code, not when i search a word that is not in the list. What may be the problem?
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,156
    Rep Power
    183
    Try this; it does what your wanting to do:
    Code:
    <script>
    
    var Autocomplete = ["an apple","alligator","elephant","pear","kingbird","kingbolt","kingcraft","kingcup","kingdom","kingfisher","kingpin","SML"];
    
    var found;
    
    function inArray(this_array,key)
    {
    found = false;
     for(i=0;i<this_array.length;i++) {
      if(this_array[i] == key){
       found = true;
      }
     }
    }
    
    (function(){
    
    inArray(Autocomplete,"elephant");
    
    // validation - begin
    
      if(found == true) {
       alert("ok!!!!!!");
      }
      else {
       alert("error");
      }
    
    // validation - end
    
    })();
    
    </script>

    Comments on this post

    • laua agrees
    Last edited by web_loone08; July 19th, 2013 at 09:56 PM.

IMN logo majestic logo threadwatch logo seochat tools logo