21

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 : !!~

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105

4 Answers4

56

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
};
gion_13
  • 41,171
  • 10
  • 96
  • 108
  • Your first `if` assumes that `-1` is treated as a falsy value? Shouldn't it be `!= -1`? +1 for the complete answer. – Fabrício Matté Jul 15 '12 at 11:28
  • thanks, I was just modifying the code snippet and it must've slipped. – gion_13 Jul 15 '12 at 11:30
  • ok, thanks, it works great . but i have a clarification question - so there is no way to use the `inArray()` function without comparing it to `==` -1 , `== 0` or similar ?? – Obmerk Kronen Jul 15 '12 at 11:56
  • no, bu if you really want to, you can make your own wrapper function : `function notInArray(){return jQuery.inArray.apply(this,arguments)!=-1;}` which takess the same argumets as the initial function and returns a boolean (true if the element is not in the array); – gion_13 Jul 15 '12 at 12:01
  • thanks . it all works great . I am a real novice in JS . 90% of he time those are syntax errors :-) I am glad that now it wasn´t :-) – Obmerk Kronen Jul 15 '12 at 12:26
1

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

Inkbug
  • 1,682
  • 1
  • 10
  • 26
0

Alternate solution of the values check

//Duplicate Title Entry 
    $.each(ar , function (i, val) {
        if ( jQuery("input:first").val()== val) alert('VALUE FOUND'+Valuecheck);
  });
Akxaya
  • 834
  • 8
  • 6
-1

The Array.prototype property represents the prototype for the Array constructor and allows you to add new properties and methods to all Array objects. we can create a prototype for this purpose

Array.prototype.has_element = function(element) {
    return $.inArray( element, this) !== -1;
};

And then use it like this

var numbers= [1, 2, 3, 4];
numbers.has_element(3) => true
numbers.has_element(10) => false

See the Demo below

Array.prototype.has_element = function(element) {
  return $.inArray(element, this) !== -1;
};



var numbers = [1, 2, 3, 4];
console.log(numbers.has_element(3));
console.log(numbers.has_element(10));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68