Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Am I using false the correct way?

    Does anyone know why I am not getting an alert on an empty form INPUT value?

    Code:
    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.

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Noonga View Post

    I suspect the problem is due to 'false'..
    spot on

    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.

  • Users who have thanked webdev1958 for this post:

    Noonga (03-16-2012)

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Code:
    if (document.forms["form"]["targetname"].value == "" && document.forms["form"]["unknownname"].checked == false) {
    	alert ("What ever");
    	return false;
    }
    Solved.
    Last edited by Noonga; 03-16-2012 at 01:02 AM.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Noonga (03-16-2012)

  • #5
    New Coder
    Join Date
    Feb 2012
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Its done now, but will use the method next time. Thanks.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •