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 Improving the form validation

jQuery question

Hello,

I'm being asked to complete the following code challenge in the 'Query Basics course:

On line 11 in app.js, implement the isUsernamePresent function. Return true if the username length is greater than 0.

The error I'm getting is: Bummer! There was an error with your code: TypeError: '[object Object]' is not a function (evaluating '$username()')

The code I have is supposed to be attached. Thanks for any help you can provide.

Jim Gravitt

js/app.js
//Problem: Hints are shown even when form is valid
//Solution: Hide and show them at appropriate times
var $password = $("#password");
var $confirmPassword = $("#confirm_password");
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();
}

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


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

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

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

enableSubmitEvent();
index.html
<!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">
            <span>Please enter username</span>
        </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="//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>

3 Answers

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Jim, $username isn't a function, you've accidentally added brackets to the end :)

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

Thanks. Any idea about what code I need to "implement the isUsernamePresent function. Return true if the username length is greater than 0." as the code challenge is requesting? The correct code is supposed to be placed on one line, line 11, right after the line

function isUsernamePresent() {

Thanks for any help you can provide. Jim Gravitt

Thanks. Any idea about what code I need to "implement the isUsernamePresent function. Return true if the username length is greater than 0." as the code challenge is requesting? The correct code is supposed to be placed on one line, line 11, right after the line

function isUsernamePresent() {

Thanks for any help you can provide. Jim Gravitt

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Jim, I'm not entirely sure I understand your question? The code I posted will get you past the first part of the challenge.

Thanks for your help. I was able to answer the second part of the challenge. Thanks again.

Jim Gravitt