kk luo
kk luo

Reputation: 599

why I can not use && in javascript for not blank and not space

I want to check the value is not blank or one empty space so I wrote a code

var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value != " " && OccLocation.value != "") {
  alert("not empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass"
/>

Upvotes: 0

Views: 106

Answers (4)

Mamun
Mamun

Reputation: 68933

Your condition is wrong.

  1. You have to use == instead of !=.
  2. If you use && then both condition should be true to return true, which is ultimately impossible at the same time in this case. Use || instead, this will be evaluated as true if any of the condition is true.

The condition should be:

if (OccLocation.value ==" " || OccLocation.value == "")

Even you can simplify the condition by using String.prototype.trim() :

The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

Try

if (OccLocation.value.trim() ==  "")

var OccLocation = document.getElementById("HdnOccLocation");

if (OccLocation.value.trim()== ""){
  alert ("empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass" />

Upvotes: 1

Mark Schultheiss
Mark Schultheiss

Reputation: 34168

Your code runs immediately, and the value="" sets it to empty. Here, I set the value in the markup so it has some, thus it alerts.

var OccLocation = document.getElementById("HdnOccLocation");
console.log(OccLocation.value)
if (OccLocation.value != " " && OccLocation.value != "") {
  alert("not empty");
}
<input type="hidden" id="HdnOccLocation" name="HdnOccLocation" value="dd" style="position:absolute;height:20px;color:#000000;text-align:left;font-size:12px;font-style:normal;width:26px;background-color:#00cccc;left:800px;font-weight:normal;top:220px;" class="textClass"
/>

Upvotes: 0

Raheela Aslam
Raheela Aslam

Reputation: 482

You can update your condition as below.

var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim() == "") {
    alert("empty");
}

If you want to get alert if OccLocation is not empty then :

var OccLocation = document.getElementById("HdnOccLocation");
if (OccLocation.value.trim() != "") {
    alert("not empty");
}

Upvotes: 1

Seth Flowers
Seth Flowers

Reputation: 9190

You are checking that it is not empty, then alerting that it is empty. I think you mean to check that it is empty. Change your JS to the following:

var OccLocation = document.getElementById("HdnOccLocation");

if (OccLocation.value === " " || OccLocation.value === "")
{
    alert ("empty");
}

Upvotes: 0

Related Questions