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

I don't know why this isn't working right

Something isn't working right with my code and I can't figure it out. As far as I can see, my code looks like it is right. However, when I was at the part in the video where I check my developers tools, it was showing canSubmit(); what I got was this instead of "false":

function canSubmit() {
Return isPassword Valid() && arePasswordsMatching();

Even when I made the change from "canSubmit();" to "enableSubmitEvent();" and refreshed the page, it didn't go away in the console part of the developer tools like it did for Andrew in the video. Plus, I never saw the 405 warning at any point when I ran the program in Workspaces. Any help on this is greatly appreciated. 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 matched
        $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
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent)
.keyup(enableSubmitEvent);  

enableSubmitEvent();
<!DOCTYPE html>
<html>
<head>
    <title>Sign Up Form</title>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" title="no title" charset="utf-8">
</head>
<body>

    <form action="#" method="post">
        <p>
            <label for="username">Username</label>
            <input id="username" name="username" type="text">
        </p>
        <p>
            <label for="password">Password</label>
            <input id="password" name="password" type="password">
            <span>Enter a password longer than 8 characters</span>
        </p>
        <p>
            <label for="confirm_password">Confirm Password</label>
            <input id="confirm_password" name="confirm_password" type="password">
            <span>Please confirm your password</span>
        </p>
        <p>
            <input type="submit" value="SUBMIT" id="submit">
        </p>
    </form>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/app.js" type="text/javascript" charset="utf-8"></script>
</body>
</html>

1 Answer

Steven Parker
Steven Parker
229,708 Points

It sounds like you just named canSubmt (no parentheses) instead of invoking it: canSubmit().

Once I did what you suggested, that did work, But, it still isn't working the way it should. I typed in everything else in the developer tools as the video instructed and the this is what I ended up with:

$("#shift")
[<input type="submit" value="SUBMIT" id="submit" disabled>]
$("#shift").prop("disabled", true)
[<input type="submit" value="SUBMIT" id="submit" disabled>]

This happens even when I refresh the page after changing "canSubmit();" to "enableSubmitEvent();. I don't know why it it is doing this. All of that stuff should have disappeared when I refreshed the page. Did I leave something out to cause this to happen?