5

FYI: this is for a simple quiz with just a single input field for each answer.

I have the following Javascript if statement to check if the value entered into an input field is correct (in this case, if the value entered is 'england').

$('input').keyup(function () {
    if ($(this).val().toLowerCase() == 'england') {
        //Stuff
    } else {
        //Other Stuff
    };
});

However, I want to allow for alternative spellings, so I need a few possible answers for each question - it seems sensible to use an array for this as so...

var ans1 = new Array();
ans1[0] = "England";
ans1[1] = "Englund";
ans1[2] = "Ingland";

How can I change my if statement to say 'if the input field value equals any of those values from the array, then do the following'?

Any help would be greatly appreciated! Thank you.

user2586455
  • 591
  • 2
  • 7
  • 16

6 Answers6

6

You can do this using .inArray():

if ($.inArray($(this).val(), ans1) > -1) {
    //Stuff
}

Here, the code $.inArray($(this).val(), ans1) will search for a specified value for example England within an array ans1 and return its index (or -1 if not found).

UPDATE

For case-sensitive search:

  • First enter all the values in the array in Lower Case
  • Next use the code below:-

JS:

if ($.inArray($(this).val().toLowerCase(), ans1) > -1) {
    //Stuff
}
palaѕн
  • 72,112
  • 17
  • 116
  • 136
4

You can use the 'indexOf' method of the array, this will return -1 if the value doesn't exist in the array:

//if answer is in array
if(array.indexOf(answer) != -1){
    //do stuff
}else{
  //do stuff
}
georg
  • 211,518
  • 52
  • 313
  • 390
James
  • 2,195
  • 1
  • 19
  • 22
3

Try this

if(this.value.match(/^(England|Englund|Ingland)$/i))

using regex and gi modifier for case insensitive

Anton
  • 32,245
  • 5
  • 44
  • 54
0

Do like this

$('input').keyup(function () {

var ans1 = new Array();
ans1[0] = "England";
ans1[1] = "Englund";
ans1[2] = "Ingland";
for(int i=0;i<ans1.length;i++)
{

    if ($(this).val().toLowerCase() ==ans1[i]) {
        //Stuff
    } else {
        //Other Stuff
    };
}
});
Bharath R
  • 1,491
  • 1
  • 11
  • 23
0

Perhaps you may consider checking each element of the array like that:

var ans1 = new Array();
ans1[0] = "England";
ans1[1] = "Englund";
ans1[2] = "Ingland";

$('input').keyup(function () {
    for (var i = 0; i < ans1.length; i++) {
       if ($(this).val().toLowerCase() == ans1[i]) {
        //Stuff
         } else {
        //Other Stuff
       };
    }
});

Not the most beautiful solution, but it should work.

briosheje
  • 7,356
  • 2
  • 32
  • 54
0

jQuery offers $.inArray:

var found = $.inArray('specialword', words) > -1;

Note that inArray returns the index of the element found, so 0 indicates the element is the first in the array. -1 indicates the element was not found.

put your spellings in an array like this:

words: [
    "England"
    "Inglund"
    "Ingland"
]

Found will be true if the word was found.

If you want the index of the matched word delete > -1 from the line.

Your code would be like this:

$('input').keyup(function () {
    var found = $.inArray($(this).val(), words);
    found > -1 ? //Stuff : //otherStuff;

});
Igl3
  • 4,900
  • 5
  • 35
  • 69