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

Daniel Sarmiento
Daniel Sarmiento
10,265 Points

Password validation and password confirmation are not working after arePasswordsMatching() and isPasswordValid() applied

I was following along in workspaces and in min 3:30 Andrew showed the form working properly, but I haven't been able to make it work and I think I have the same code as his!

If you can see what's wrong with my code please tell me.

// problem: hints are shown even when form is valid
// solution: hide and show them at appopiate times
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();
}

function passwordEvent() { 
    // find out if the 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 the hint
    $confirmPassword.next().show();
  }  
}

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

// when event happens on confirmation input
$confirmPassword.focus(confirmPasswordEvent()).keyup(confirmPasswordEvent());
Daniel Sarmiento
Daniel Sarmiento
10,265 Points

I've just solved this. I was calling functions inside methods "keyup" and "focus" with "()" at the end.

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

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

The fix

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

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

Silly syntax error but got the scratching my head for a while.

1 Answer

Steven Parker
Steven Parker
230,688 Points

Congratulations on resolving your own issue. :+1: