1

I have the following:

var NewCount = document.getElementById('MainContent_gv_NewCount_' + rowIndex).value;
if (NewCount != "") {
    document.getElementById('MainContent_gv_lblTotal_' + rowIndex).innerHTML = "£" + ((originalCount - NewCount) * unitCost).toFixed(2);
} else { 
   document.getElementById('MainContent_gv_lblTotal_' + rowIndex).innerHTML = "";
            }

The calculations I am doing are based on the value in the textbox. (NewCount).

I want the label to update if the value is any number (including 0), but to be wiped if the user clears the textbox. However, at the moment it is treating a blank textbox and a textbox with 0 in it the same.

How can I differentiate between the two?

Ben
  • 4,281
  • 8
  • 62
  • 103
  • 1
    I don't believe that the code you posted could possibly be responsible for what you perceive to be going wrong. The comparison you've got, `NewCount != ""`, will be `true` when the value of the input box is any non-empty string, including the string "0". – Pointy Feb 04 '11 at 13:48
  • Are you wanting to treat white space the same as "" ? – JoeyRobichaud Feb 04 '11 at 13:53
  • @Pointy - That's not how `!=` works. You need `!==`. – OrangeDog Feb 04 '11 at 14:18

4 Answers4

3

Use !== in your if statement.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

I can't reproduce the behavior you are describing. In my tests a textbox with "0" in it will be considered not blank by Javascript using your comparison logic (!= "").

Here is my attempt: http://jsfiddle.net/pJgyu/5404/

joshuapoehls
  • 32,695
  • 11
  • 50
  • 61
0
if ( NewCount.length == 0 ) {
    // text-box is empty
} else if ( !isNaN(NewCount) ) {
    // the value is a number
}
Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
0

Any of the following could work

NewCount.length > 0
NewCount !== ''
Nalum
  • 4,143
  • 5
  • 38
  • 55