...

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



Noonga
03-16-2012, 12: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.

webdev1958
03-16-2012, 12: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.

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


Solved.

felgall
03-16-2012, 02: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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum