I am trying to check if my array regions contains part of a string that users submit. In essence, this jquery script should check if the city that a user included in their address is one of the cities in the regions array.
For example, when a user enters Examplestreet 24 City1 and City1 is in the regions array, it should display a price of €40, else it should show €2/km.
I have the following code:
var regions = ["city1", "city2", "city3"];
var str = $("#addressField").val();
var address = str.toLowerCase();
var key, value, result;
for (key in regions) {
if (regions.hasOwnProperty(key) && !isNaN(parseInt(key, 10))) {
value = regions[key];
if (value.substring() === address) {
$("#deliveryPrice").text("€40");
}
else {
$("#deliveryPrice").text("€2/km");
}
}
}
This code is working fine when the string is just the city without the street or other characters, but it should also work if someone enters their full address. So I need to change my code so it searches the array regions for any part of the string address.