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

Okay, I'm stuck. Challenge 2 of 2 on perfecting the jquery form,on the canSubmit(). I understand, but I can't pass.

here's code:

var $username = $("#username");

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

function isUsernamePresent() {
   return $username.val().length > 0;
}

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

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

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

function passwordEvent(){
}

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());
}


function usernameEvent() {
  if(isUsernamePresent()) {
    $username.next().hide();
  } else {
    $username.next().show();
  } 
}

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

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

$username.focus(usernameEvent).keyup(usernameEvent).keyup(enableSubmitEvent);

enableSubmitEvent();

Oh, also, don't we have to add the .keyup for isUserNamePresent?

1 Answer

Hey Maureen O'Neal,

To answer the first part of your question, you have to compare what you wrote to call the function in the return and the name of the function itself. You wrote "isUserNamePresent()" in the return but the actual name of the function is "isUsernamePresent()". That one capital N there was your downfall! =P

As for the second part, the validation presented here is checking each time the user has let go of a key on the keyboard after focusing on those form elements. So, the "enableSubmitEvent()" function is firing off multiple times during the course of typing in things. It will only be enabled, though, when all of the functions it calls on return "true" because it reverses the value with that ! symbol such that if it gets a true back, it will set to "disabled" to false.

I don't know if you've done much with logic gates, but the && operator will only return true when all booleans attached to that gate are true; otherwise, it will return false. i.e.

true && true && true == true
true && true && false == false
false && false && true == false
false && true && false == false
true && false && false == false
etc. etc.

I hope that makes sense! :)

And, if you use Google Chrome or Firefox, you can open up the Developer Tools and go into the JavaScript console to test any logic gates for yourself. Just type in the expression such as true && true && false and it will return the answer.

Great, thank you so much, I thought it was easy, but I was worrying about adding keyup in there too! Thanks so much! :)

Anytime, Maureen! =] If you have any more questions, I, or another knowledgeable member of Treehouse, will be more than happy to help.