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 trialEdgar Noszczynski
4,540 PointsWhy my $username checking dont want to work?
var $password = $('#password');
var $username = $('#username');
var $empety = '';
var $confirmPassword = $('#confirm_password');
$("form span").hide();
function usernamePresent() {
return $username.val() === $empety;
}
function isPasswordValid() {
return $password.val().length > 8;
}
function arePasswordsMatching() {
return $password.val() === $confirmPassword.val();
}
function canSubmit() {
return isPasswordValid() && arePasswordsMatching() && usernamePresent();
}
function passwordEvent() {
if(isPasswordValid()) {
$password.next().hide();
} else {
$password.next().show();
}
}
function confirmPasswordEvent () {
if(arePasswordsMatching()) {
$confirmPassword.next().hide();
} else {
$confirmPassword.next().show();
}
}
function enableSubmitEvent() {
$('#submit').prop('disabled', !canSubmit());
}
$password.focus(passwordEvent).keyup(passwordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);
$confirmPassword.focus(confirmPasswordEvent).keyup(confirmPasswordEvent).keyup(enableSubmitEvent);
enableSubmitEvent();
//I just add
//&& usernamePresent(); + var empety = '';
// and function
//function usernamePresent() {
return $username.val() === $empety;
}
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Edgar,
You want to check that the username is not empty.
return $username.val() !== $empety;
Also, it would make your code more readable to check against an empty string rather than using a variable to store an empty string.
return $username.val() !== '';
Edgar Noszczynski
4,540 PointsHi Jason, But it's not working, button is disable.
Its working but only after few 'enter' press on active username input
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsI'm sorry but I forgot that there was more code to this.
I don't think adding username validation was covered in the videos but it's covered in the next code challenge. So you'll want to look over that code and see what has been added.
For now, you'll need to add the keyup event to the username field.
$username.keyup(enableSubmitEvent);
This way you can run the
canSubmit
function after you've types something in the username field.More is added to this in the code challenge so look that over and compare to your code in order to fully implement the username validation.