View Full Version : Am I using false the correct way?

03-16-2012, 01:40 AM
Does anyone know why I am not getting an alert on an empty form INPUT value?

if (document.forms["form"]["targetname"].value == "" && document.forms["form"]["unknownname"] == false) {
alert ("What ever");
return false;

document.forms["form"]["targetname"] is a form INPUT text
document.forms["form"]["unknownname"] is a form INPUT checkbox

I suspect the problem is due to 'false'.. but I can't figure out why.

03-16-2012, 01:48 AM
I suspect the problem is due to 'false'..

spot on :thumbsup:

You need to see if the checked property of the checkbox is true or false. Your code has just the object reference to the checkbox.

03-16-2012, 01:55 AM
if (document.forms["form"]["targetname"].value == "" && document.forms["form"]["unknownname"].checked == false) {
alert ("What ever");
return false;


03-16-2012, 03:17 AM
There's no point in using

== false

as simply putting a ! in front of what you are comparing it with will achieve the same result in less code.

Alternatively you could use

=== false

as that doesn't have a shorter equivalent and also does a more accurate test as null, undefined, 0 and '' are == false but not === false

03-16-2012, 10:32 AM
Its done now, but will use the method next time. Thanks.