Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS jQuery - check if value is in array

I am more of a PHP person, not JS - and I think my problem is more a syntax problem ..

I have a small jQuery to "validate" and check input value .

It works ok for single words, but I need array.

I am using the inArray() of jQuery .

var ar = ["value1", "value2", "value3", "value4"]; // ETC...

        jQuery(document).ready(function() {

            jQuery("form#searchreport").submit(function() {
            if (jQuery.inArray(jQuery("input:first"), ar)){ 
                      //if (jQuery("input:first").val() == "value11") { // works for single words
            jQuery("#divResult").html("<span>VALUE FOUND</span>").show();
            jQuery("#contentresults").delay(800).show("slow");
                return false;
              }

        // SINGLE VALUE SPECIAL CASE / Value not allowed 
               if (jQuery("input:first").val() == "word10") {

                jQuery("#divResult").html("YOU CHEAT !").show();
                jQuery("#contentresults").delay(800).show("slow");

                return false;
              }

        // Value not Valid

              jQuery("#divResult").text("Not valid!").show().fadeOut(1000);

              return false;
            });

        });

now - this if (jQuery.inArray(jQuery("input:first"), ar)) is not working right .. every value that I put will be validated as OK . (even empty)

I need to validate only values from the array (ar) .

I tried also if (jQuery.inArray(jQuery("input:first"), ar) == 1) // 1,0,-1 tried all

what am i doing wrong ?

Bonus question : how to do NOT in array in jQuery ?? (the equivalent of PHP if (!in_array('1', $a)) - I sw somehre that it will not work , and need to use something like this : !!~

like image 972
Obmerk Kronen Avatar asked Jul 15 '12 11:07

Obmerk Kronen


People also ask

How do you check if a value is in an array jQuery?

inArray( value, array [, fromIndex ] )Returns: Number. Description: Search for a specified value within an array and return its index (or -1 if not found).

How do you check if a value exists in an array JavaScript?

JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found.

How do you check if an item exists in an array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if an array of object contains a value?

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.


2 Answers

You are comparing a jQuery object (jQuery('input:first')) to strings (the elements of the array).
Change the code in order to compare the input's value (wich is a string) to the array elements:

if (jQuery.inArray(jQuery("input:first").val(), ar) != -1)

The inArray method returns -1 if the element wasn't found in the array, so as your bonus answer to how to determine if an element is not in an array, use this :

if(jQuery.inArray(el,arr) == -1){
    // the element is not in the array
};
like image 54
gion_13 Avatar answered Oct 11 '22 18:10

gion_13


As to your bonus question, try if (jQuery.inArray(jQuery("input:first").val(), ar) < 0)

like image 1
Inkbug Avatar answered Oct 11 '22 19:10

Inkbug