Question

I cannot find out how to pass multiple strings in an If statement.

Here is my Code :

    var date = new Date();

    if (document.getElementById("postcode-entry").value == ("G74" || "G75")) {
        if (date.getHours() < 8 ) {
            window.alert("Sorry we are not open at the moment, please try again later.");
        } else {
            window.open("http://http://stackoverflow.com");
        }
    } else {
        window.alert("Sorry we do not Delivery to your area, please collect from store");
    }

How can i do this ?

Was it helpful?

Solution

The phrase ("G74" || "G75") forces a boolean evaluation on each string, and both will return true always.

So you would need to do something like this:

var myvar = document.getElementById("postcode-entry").value;

if(myvar === "G74" || myvar === "G75")

OTHER TIPS

i am not sure if you want to follow this approach but try using the following-

var strArr = [ 'G74', 'G75' ];

if( strArr.indexOf( document.getElementById("postcode-entry").value ) !== -1 ) {
// Your  normal code goes  here
}

Using this, you can have n number of string tested in a single statement inside if.

This should do

 var post_code = document.getElementById("postcode-entry").value;
  if (post_code == "G74" || post_code == "G75") 

I have never seen this before. Perhaps you can use the switch statement

But in your case I would recomment the following:

var poscode = document.getElementById("postcode-entry").value
if (postcode === "G74" || postcode === "G75") ......
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top