1

this is my first time trying to incorporate an array into my javascript code. I am using this with a form that contains a text input in which only the colors Blue, Yellow, Gray, Orange or Pink can be submitted, otherwise there should be an alert. Clearly I have made some error as my values are not being picked up...anything can be entered into the input #color and the form can still be submitted, no alert ever populates regardless of what I enter. Code is below. Thank you.

    }   
  // Verifies Colors 
  var colors = ["Blue", "Yellow", "Gray", "Orange", "Pink"];
      if ($('#color').val() != colors) {
          alert("These are not the right colors!");
          return false;       
        }
 return true;
}
2
  • Can you post the whole function? If it's not too large of course. There are brackets here that don't have any meaning. Commented Sep 30, 2013 at 23:16
  • You're comparing a single element's value to an entire Array of "possible" colors. You need iterate through the colors and see if any of them are being used. If so, return a true and all is well, else return a false. Commented Sep 30, 2013 at 23:18

2 Answers 2

1

try

if(colors.indexOf($("#color").val()) == -1 ){
    alert("These are not the right colors!");
    return false;
}

[edit] This is an assumption, I think #colors is a input type="text", or a select, in either case, I would suggest to pay attention to the capitalization of words, considering that the input might be all lowercase or all uppercase or a MiX oF BoTh.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much Rafael! This works perfectly and I appreciate your point about variations on input. I will definitely have to address that somehow. Regards, Jenny (zipotontic)
1

Since you're already using jquery.

var colors = ['blue', 'red', 'whatever'];
if ($.inArray($("#color").val(), colors) < 0) {
    alert("These are not the right colors!");
    return false;
}

To Rafael's, and Kyle's, point though jquery will use indexOf if it can, from the jquery source

inArray: function( elem, arr, i ) {
    var len;

    if ( arr ) {
        if ( core_indexOf ) {
            return core_indexOf.call( arr, elem, i );
        }

        len = arr.length;
        i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;

        for ( ; i < len; i++ ) {
            // Skip accessing in sparse arrays
            if ( i in arr && arr[ i ] === elem ) {
                return i;
            }
        }
    }

    return -1;
}

Where core_indexOf is an alias for array.indexOf

core_indexOf = core_deletedIds.indexOf

4 Comments

Had to edit it. Less than zero is false, not greater than -1 (What I had originally). Check out jQuery docs api.jquery.com/jQuery.inArray
might be worth noting that using indexOf is quicker than using $.inArray if the browser supports it. See this SO post: stackoverflow.com/questions/1883663/should-i-use-jquery-inarray
Any JS native function is probably faster than <library name here>. This was just the fastest thing I could come up with and I was uncertain if indexOf worked like that. The only example I found was w3c and for string manipulation.
I would like to add that indexOf is not supported in IE8. Casually, today I had a problem with that at work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.