0

I have a quick question and I feel like I might just be missing something easy.

I currently have an array of codes below the document ready function of my jquery

    $(document).ready(function() { 
var code = ['code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'code', 'HG3BV', 'code', 'code', 'code', 'code', 'code', 'code'];
"use strict";

This is at the top of my js document.

Then down further I check on my forms if it finds a class value of validate-required2 and if it does, I want it to check the value of the textbox #codes against my array.

            $(form).find('.validate-required2').each(function() {
            var cheater = $('#codes').val();
              if (jQuery.inArray(cheater, code)!='-1') {
                $(this).removeClass('field-error');
            } else {
                $(this).addClass('field-error');
                error = 1;
            }
        });

However this is constantly returning that the values aren't in the array. Did I miss something easy here?

2
  • put your code array declaration outside $(document).ready, before it. Commented Aug 17, 2015 at 20:01
  • Is the code in the same case as the array? Commented Aug 17, 2015 at 20:02

1 Answer 1

1

You probably meant to write

var cheater = $(this).val();

if the selector '.validate-required2' refers to your input-elements. Your original selector can only get you one value in total since the id referred to ('#codes') must be unique on your page.

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

1 Comment

I actually just forgot to change the id on my input box to codes which was a really dumb thing of me to do but your answer works too!

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.