Search code examples
javascriptvalidationinclude

parameter.includes() is not a function


I'm pretty new at js programming. I'm developing an admission form as part of our project to be submitted. It's also my first time asking a question here. I'm creating a form of validation to prevent any invalid values to be entered. I also need some optimizations at my code. If you have any suggestions to make it shorter, I'll be glad to accept your suggestion too.

I tried executing the matchCheck() function on other scenarios, but it just works fine. I also tried executing validateDate() on the console and other scenarios, but it also worked without any errors. I got an error when the functions are executed at if statements.

Here is the error message: Unknown TypeError: arrayToBeChecked.includes is not a function

I got an error at these function and if statements:

function matchCheck(arrayToBeChecked, findingValue) {
    return arrayToBeChecked.includes(findingValue);
}
if (matchCheck(date[0], "null") === false)
if (validateDate(bdate) === true)

Here is the code (Excluded some of the unrelated variables and codes):

//Check Match
function matchCheck(arrayToBeChecked, findingValue) {
    return arrayToBeChecked.includes(findingValue);
}
//Date Validator
//Expected Format [MM/DD/YYYY]
function validateDate(date) {
    //check if the date is valid
    var leapYear = date[2] % 4;
    var mos31 = ["1", "3", "5", "7", "8", "10", "12"];
    var mos30 = ["4", "6", "9", "11"];
    var flyInv = ["30", "31"];
    var fnlyInv = ["29", "30", "31"];
    var mos30Inv = "31";

    if (matchCheck(date[0], "null") === false) {
        if (matchCheck(date[1], "null") === false) {
            if (matchCheck(date[2], "null") === false) {
                if (leapYear == 0) {
                    if (date[0] == "2") {
                        if (matchCheck(flyInv, date[1]) === false) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                    else if (matchCheck(mos31, date[0]) === true) {
                        return true;
                    }
                    else if (matchCheck(mos30, date[0]) === true) {
                        if (matchCheck(mos30Inv, date[1]) === false) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                    else {
                        return false
                    }
                }
                else {
                    if (date[0] == "2") {
                        if (matchCheck(fnlyInv, date[1]) === false) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                    else if (matchCheck(mos31, date[0]) === true) {
                        return true;
                    }
                    else if (matchCheck(mos30, date[0]) === true) {
                        if (matchCheck(mos30Inv, date[1]) === false) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    }
                    else {
                        return false;
                    }
                }
            }
            else {
                return false;
            }
        }
        else {
            return false;
        }
    }
    else {
        return false;
    } 
}
//Contact Number Validator
//Expected Format [09XXXXXXXXX]
function cnValid(nos) {
    if (nos.value.length === 11) {
        if(nos.indexOf("09") > -1) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        return false;
    }
}
//Check for empty selects
function checkEmptySelects(el) {
    var selects = document.querySelectorAll(el);
    var i;
    for (i = 0; i < selects.length; i++) {
        if (selects[i].value == "0") {
            return true;
        }
        else {
            return false;
        }
    }
}

//Valid Relation Values
var vrv = ["mother", "father", "grandmother", "grandfather", "auntie", "uncle", "housemaid", "Mother", "Father", "Grandmother", "Grandfather", "Auntie", "Uncle", "Housemaid", "Aunt", "aunt", "brother", "Brother", "sister", "Sister", "cousin", "Cousin"];

//Start Review
function reviewForm() {
    var letters = /^[a-zA-Z\s]*$/;
    var mailFormat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    var noScFormat = /[^a-zA-Z0-9\\,\\.\s]/g;

    //Perform Checks
    if (checkEmptySelects("select") === false) {
        if (ln.value.match(letters)) {
            if (fn.value.match(letters)) {
                if (mn.value.match(letters)) {
                    if (eml.value.match(mailFormat)) {
                        if (age.value >= 0) {
                            if (age.value <= 100) {
                                if (validateDate(bdate) === true) {
                                    if (pob.value.match(noScFormat)) {
                                        if (ca.value.match(noScFormat)) {
                                            if (rlg.value.match(letters)) {
                                                if (nsl.value.match(letters)) {
                                                    if (cmn.value.match(letters)) {
                                                        if (mo.value.match(letters)) {
                                                            if (cfn.value.match(letters)) {
                                                                if (fo.value.match(letters)) {
                                                                    if (gn.value.match(letters)) {
                                                                        if (matchCheck(vrv, rts) === true) {
                                                                            if (cnValid(cn) === true) {
                                                                                if (lrn.value.length === 12) {
                                                                                    //Submit Data to db if all of the requirements are passed.
                                                                                }
                                                                                else {
                                                                                    //Error Message;
                                                                                }
                                                                            }
                                                                            else {
                                                                                //Error Message
                                                                            }
                                                                        }
                                                                        else {
                                                                            //Error Message
                                                                        }
                                                                    }
                                                                    else {
                                                                        //Error Message
                                                                    }
                                                                }
                                                                else {
                                                                    //Error Message
                                                                }
                                                            }
                                                            else {
                                                                //Error Message
                                                            }
                                                        }
                                                        else {
                                                            //Error Message
                                                        }
                                                    }
                                                    else {
                                                        //Error Message
                                                    }
                                                }
                                                else {
                                                    //Error Message
                                                }
                                            }
                                            else {
                                                //Error Message
                                            }
                                        }
                                        else {
                                            //Error Message
                                        } 
                                    }
                                    else {
                                        //Error Message
                                    }
                                }
                                else {
                                    //Error Message
                                }
                            }
                            else {
                                //Error Message
                            }
                        }
                        else {
                            //Error Message
                        }
                    }
                    else {
                        //Error Message
                    }
                }
                else {
                    //Error Message
                }
            }
            else {
                //Error Message
            }
        }
        else {
            //Error Message
        }
    }
    else {
        //Error Message
    }   
}

Solution

  • The error occurs when the "arrayToBeChecked" value that is passed to the "matchCheck()" function is not an arry. To fix this, you could convert "arrayToBeChecked" to an array if it's not already an array.

    function matchCheck(arrayToBeChecked, findingValue) {
      // Convert arrayToBeChecked to an array if it's not already an array
      if (!Array.isArray(arrayToBeChecked)) {
        arrayToBeChecked = [arrayToBeChecked];
      }
    
      return arrayToBeChecked.includes(findingValue);
    }