Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript jQuery Basics (2014) Creating a Password Confirmation Form Perform: Part 2

Dan Witts
Dan Witts
3,720 Points

My confirm password tab wont disappear when I type 8 characters or more!! what am I doing wrong?

//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 passwordEvent(){
  //Find out if password is valid
 if($password.val().length > 8) {
//Hide hint if valid
$password.next().hide();

} else {
  //else show hint
$password.next().show();

  }
}

function confirmPasswordEvent() {
//Find out if password and conformation match
if($password.val() === $confirmPassword.val()) {
  //Hide hint if match
$confirmPassword.next().hide();
} else {
  //else show 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);

Edited to format code for clarity.

5 Answers

try using siblings("span") instead of next() which searches for the other children within the same parent container and locates the ones which are span elements. i.e

function passwordEvent(){ //Find out if password is valid if($password.val().length > 8) { //Hide hint if valid $password.siblings("span").hide();

} else { //else show hint $password.siblings("span").show();

} }

function confirmPasswordEvent() { //Find out if password and conformation match if($password.val() === $confirmPassword.val()) { //Hide hint if match $confirmPassword.siblings("span").hide(); } else { //else show hint $confirmPassword.siblings("span").show();

}

}

rydavim
rydavim
18,785 Points

In your code, you don't seem to be checking the length of the confirmPassword field. You established a valid password in the first field, and are confirming that the second field value matches the first. Since the password value must be at least 8 characters long before the first hint tab goes away, if the second one matches it must be more than 8 characters as well.

Hopefully that makes sense, but please let me know if you have any further questions. Happy coding! :)

Dan Witts
Dan Witts
3,720 Points

It works! thank you Dylan and everyone else for helping out

No worries

No worries