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

Trent Burkenpas
Trent Burkenpas
22,388 Points

Submit Button is still going through

Hey there must be something wrong with my code, because the submit button is still going through even though the password and confirm password do not match.

here is my code:

//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()) {
    //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 ebent happens on confirmation input
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);

enableSubmitEvent();

Thanks!

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Trent Burkenpas, so I just opened the project up in a workspace and found 2 problems with the default html that my be addressed in the video (I haven't had a chance to watch them yet so I don't know). First, make sure you submit button has the id="submit". Second, make sure you add the disabled attribute to the submit button. After I made those changes you code worked fine.

Trent Burkenpas
Trent Burkenpas
22,388 Points

Thanks, Its always those stupid little mistakes!