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!

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

Added the #username as one of the prerequisites to submit. Critique my work, please?

Here's my continuing what Andrew suggested:

//Problem: Hints are shown even when form is valid
//Solution: Hide and show span at appropriate times

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

var $username = $("#username");
var $password = $("#password");
var $confirmPassword = $("#confirm_password");
var $submitButton = $("#submitButton");

function usernamePresent() {
    return $username.val() !== "";  

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

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

//To make sure that we can submit once the 2 conditions above (password is valid &&  password + confirmation pw are matching)
function canSubmit() {
    return isPasswordValid() && arePasswordsMatching() && usernamePresent();

function passwordEvent() {
    //Find out if password is valid
    if (isPasswordValid()) {
        //Hide hint if valid
    } else {
        //else show hint

function confirmPasswordEvent() {   
    //Find out if password & confirmation match
    if (arePasswordsMatching()) {
        //Hide hint if match
    } else {
        //else show hint

function enableSubmitEvent() {
    $submitButton.prop("disabled", !canSubmit());  

//Events to enable Submit

//When event happens on password input, hint shows

//When event happens on confirmation password input, hint shows


2 Answers

Cory Harkins
Cory Harkins
16,500 Points

I like the point of view!

Gabbie Metheny
Gabbie Metheny
33,778 Points

That's pretty much what I did, and it should work! The only thing I did differently was using the length of the username rather than comparing against an empty string. So this works, too:

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

I also added some styling to the css to make it apparent when the button was disabled, using the 'disabled' pseudoclass:

input[type="submit"]:disabled {
  background: #777;
  color: #ccc;
  box-shadow: 0 3px 0 0 #444;
  cursor: default;