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

Matthew Lee
Matthew Lee
7,164 Points

My code only seems to work correctly when the characters are typed into the confirm password box first

The password hint works perfectly, The password confirmation hint only works when that dialog box is typed in before the first password dialog box. It's pretty frustrating. It works, but only under certain circumstances.

Can't seem to figure out how/why that's happening. Think it's my code or browser? I would appreciate any help!

//Problem: Hints are shown even when form is valid.
//Solution: Hide and show them at appropriate 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 canSubmit() {
  return isPasswordValid() && arePasswordsMatching();
}

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

function confirmPasswordEvent(){
  //Find out if passwords match
  if(arePasswordsMatching()) {
     //hide hint if match 
    $confirmPassword.next().hide();
  } else {
     //Else show hint
    $confirmPassword.next().show();
  }
}

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

//When even happens on password input
$password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);

//When event happens on confirmation input
$confirm_password.focus(confirmPasswordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);

enableSubmitEvent();

This looks like a challenge. Can you give a link to it?

3 Answers

Thanks for the link. I wasn't able to duplicate your results. I open it in Chrome. I disable the cache. After a couple of tests I refresh the page just to make sure everything's back to square 1. I click in the Username box and type. Then I click in the Password box, and get a popup for Password, before typing anything. I type in a password, and as I type I get a popup for the Confirm Password box.

However, there is an error message on line 52 of app.js: $confirm_password is not defined

Matthew Lee
Matthew Lee
7,164 Points

Thanks jcorum! That fixed it! I really appreciate the help.