user2093576
user2093576

Reputation: 3092

In javascript, comparing a numeric value to null

In my javascript function, am trying to validate numeric value of variable minutes

 <s:textfield name="minutesStr" value = "%{minutesStr}" onblur="minsValidator('%{empNum}',this);" id="minutes" onkeydown="return allowNumberOnly(event);" theme="simple" cssClass="txtbox_mandatory"/>
<script>

function minsValidator(empNo,obj){

    var mins = obj.value;

    alert(mins);

    if(mins == 0){
        alert("Updated minutes should be more than 0");
        obj.value="";       
        return false;
        }


}
</script>

It works fine for values from 0 or more than zero, but if the value of minutes is null, it still goes inside the if condition and throw the alert

ie,

alert("Updated minutes should be more than 0");

How can i avoid this?

Upvotes: 0

Views: 48

Answers (1)

JCOC611
JCOC611

Reputation: 19729

If you want to check value and type, use the === operator.

if(mins === 0){
    alert("Updated minutes should be more than 0");
    obj.value="";       
    return false;
}

This will match mins = 0 but not mins = null or mins = false (or other equivalently false values).

Upvotes: 4

Related Questions