Question

I am having trouble getting 0 to verify as a selectable "number" in an input field, currently 0 doesn't count to wards the verification.

function verifyIt(){
if((Number(document.form1.baseline_du_115.value)) && document.form1.baseline_du_115.value>=0 && document.form1.baseline_du_115.value<=99){
    document.form1.submit();
    return true;
}else{
    alert("How many times per month do you drink more than 3-4 drinks on a single occasion?");
    return false;
}

} I have been told that I'm supposed to make sure that in the if statement, I need to make ==0 a choose-able input. I need 0 to not throw the else

Was it helpful?

Solution

0 == false evaluates to true in javascript, due to the concept of truthiness. That's why your if clause evaluates to false every time.

It appears that you want to verify the Number(document.form1.baseline_du_115.value) is defined. Here are some ways you can write that:

document.form1.baseline_du_115.value != null
document.form1.baseline_du_115.value !== undefined
!isNaN(Number(document.form1.baseline_du_115.value))

OTHER TIPS

What happens here is that Number(0) translates to 0, which translates to false. If you just want to check that it is a number, replace

(Number(document.form1.baseline_du_115.value))

with

!isNaN((Number(document.form1.baseline_du_115.value)))

This works because if you send something that is not a number to Number(), you will get NaN

Javascript will translate this to false if the value is 0. Thus making that If statement go Else every time

(Number(document.form1.baseline_du_115.value))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top