-2

I know the rules that 0 is equal to empty string '' in javascript. So how to make if statement to differ this? I mean, I have field where I insert amount value. For empty string I need one message to show but for 0 value another. Currently, I always catch 0 == ''.

    if (amount == '') {
       $("#validation-msg3").addClass("opux-is-visible");

       return false;
    }
    else if (amount < 5) {
        $("#validation-msg1").addClass("opux-is-visible");

        return false;
    }

EDIT. when I use === I do not get condition equals true when field is empty. What does it mean? My amount value is different type than empty string? I parse it like this:

var amount = parseInput($("#amount").val());

2 Answers2

2

Just use strict compare === that will check value AND type of operands.

Justinas
  • 41,402
  • 5
  • 66
  • 96
1

Use three equation signs. It checks the type too and 0 will be different from ''.

if (amount === '') {
   $("#validation-msg3").addClass("opux-is-visible");

   return false;
}
JuhG
  • 140
  • 10