Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript jQuery Basics (2014) Creating a Password Confirmation Form Perfect

louiecamacho
louiecamacho
10,980 Points

Andrew entered canSubmit() in the console and got FALSE, I always get TRUE even though our codes match perfectly.

How do I fix it so that I get false when I'm following along?

Here is my code

//Prob: Hints are shown even when form is valid
//Solution: Hide/Show hints when form is valid/invalid
var $password = $("#password");
var $confirmPassword = $("#confirm_password");

//Hide hints
$("form span").hide();

function isPasswordValid() {
  return $password.val().length > 8;
}

function arePasswordsMatching() {
  return $password.val() === $confirmPassword.val();
}

//Checks to see if password is valid if so, enable submit
function canSubmit() {
  return isPasswordValid && arePasswordsMatching();
}

function passwordEvent() {
  //Find out if password is valid
  if(isPasswordValid()) {
    //Hide hint if valid
    $password.next().hide();
  } else {
    //else show hint
    $password.next().show();
  }
}

function confirmPasswordEvent() {
  //Find out if password and confirmation match
  if(arePasswordsMatching()) {
    //Hide hint if match
    $confirmPassword.next().hide();
  } else {
    //else show hint
    $confirmPassword.next().show();
  }
}

function enableSubmitEvent() {
  $("#submit").prop("disabled", !canSubmit());
}

//when event happens on password input
$password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);

//when event happens on confirmation input
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);

enableSubmitEvent();

1 Answer

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,735 Points

Inside the canSubmit function you're not calling the function isPasswordValid.

function canSubmit() {
  return isPasswordValid && arePasswordsMatching();
}

If you called isPasswordValid(), and you had no password there, it would be false, because nothing doesn't have enough characters. But since you're just putting the function there without calling it, it evaluates to true because the expression is just the function, which is 'truthy'.

louiecamacho
louiecamacho
10,980 Points

OMG, I knew it had to be something hidden in plain sight, thank you!