0

To simplify my problem i rewrote the code without the parsing of CSV, but instead with a variable that holds the data.

--CODE EDIT---

$(document).ready(function() {
    var qID = 'xxx';
    var source = ['text1', 'text2', 'etc3'];
    var source2 = ['text4', 'text5', 'etc6'];
    $('#question' + qID + ' input[type="text"]').change(function() {
        var validVal = 0;
        var inputVal = $(this).val();
        // Loop through the text and test the input value
        $(source).each(function(i) {
            if (inputVal == this) { // If a match is found...
                validVal = 1;
            }
        });
        // If a valid text was entered
        if (validVal == 1) { // A valid input
            alert("GOOD");
        } else { // An invalid input
            alert("NOT GOOD");
        }

        var validVal2 = 0;
        var inputVal2 = $(this).val();

       $(source2).each(function(j) {
            if (inputVal2 == this) { // If a match is found...
                validVal2 = 1;
            }
        });
        // If a valid text was entered
        if (validVal2 == 1) { // A valid input
            alert("GOOD2");
        } else { // An invalid input
            alert("NOT GOOD2");
        }
    });
});

The script works fine for one source (var source) but i want to check in the same text field 2 variables (source, source2) that will produce different alerts.

The script is run through a limesurvey form and the input is a simple [type="text"] field.

How do I check for 2 different arrays of text in the same text field?

qwertyg
  • 127
  • 10
  • As far as I see the variable *fullArray* is global, and this could be one of the issues. You should place a *var* before to define it local. – Mario Santini Aug 12 '16 at 15:36
  • I tried it and still cannot make it to work... I dont think i can manage to have 2 or 3 $.get(url,function(data) with various url under the same $(document) ? any ideas? – qwertyg Aug 12 '16 at 17:19
  • You could have all the *$.get* you want in *$(document).ready()*. Please provide the code where you load the other csv file, and add more details about your issue. Did you got errors? What happened? – Mario Santini Aug 12 '16 at 18:21
  • I just repeat the code for the one csv parsing sequently under the same $(document).ready() renaming variables in order to have unique values. So the var testArr = new Array(); will become var testArr2 = new Array(); The $.get(url,function(data) will become $.get(url2,function(data2) etc... – qwertyg Aug 12 '16 at 18:50
  • Please edit your post to add infos. You didn't show the other *$.get* and you didn't describe what the issue is. What mean *it doesn't work*? – Mario Santini Aug 12 '16 at 18:55

1 Answers1

2

Whenever you find yourself putting counters on variable names to create a series, you need to stop and think about what you are actually doing there. Making counted variable names is always wrong.

Use arrays.

var qID = 'xxx';

var source = [];
source.push(['text1', 'text2', 'etc']);
source.push(['text1', 'text2', 'etc44']);
source.push(['text15', 'text25', 'etc454']);

$('#question' + qID + ' input[type="text"]').change(function() {
    var valid = false;
    var inputVal = $(this).val();

    $.each(source, function(i, terms) {
        $.each(terms, function(i, term) {
            valid = inputVal === term;
            return !valid;  // returning false stops the .each() loop
        });
        return !valid;
    });

    if (valid) {
        alert("GOOD");
    } else {
        alert("NOT GOOD");
    }
});

A more appealing way to express the nested loop above uses built-in methods of Array.

var valid = source.some(function (terms) {
    return terms.includes(inputVal);
});

in ES6 syntax this can be made a one-liner:

var valid = source.some(terms => terms.includes(inputVal));
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • thank you very much for your answer and time to post this code. What I am trying to acomplish however is run the if 3 times since source, source2 and source3 variables are to be checked for validation seperately and produce different alerts. I edited my code above to see what i mean. For one variable everything works as expected, but when i try to check in the same text box 3 different arrays for validation code does not work as expected. I dont get any errors but the result is not correct. – qwertyg Aug 29 '16 at 07:50
  • @qwertyg Hm... My code really is not different from yours. Nothing keeps you from checking `source[0]`, `source[1]` and `source[2]` for validation separately and with different alerts. – Tomalak Aug 29 '16 at 08:45
  • thnx again, i tried your code and it works fine as it is for one validation. I tried instead of $.each(source, function(i, terms), to search only in source[0] as: $.each(source[0], function(i, terms), then it doesnt work. Maybe a syntax error in each? If I alert source[0] I see that the correct array is displayed. What can be wrong? – qwertyg Aug 30 '16 at 07:07