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


How to check username length for form validation? JS

Trying to build a form validation with JavaScript and my mind has gone blank trying to figure this out on my own. since I don't fully understand certain things let figured I would do it and hopes to figuring this out. this is all I got for right hints will def help thanks!

// variables for selecting id and class names linked to html
const userName = document.querySelector('#username'); 
const form = document.querySelector('#form'); 
const password = document.querySelector('#password');
const verifyPassword = document.querySelector('#password2');

//Show error message
function showError(input, message){
  const controlForm = input.parentElement;
  controlForm.className = 'form-control-error';
  const small = document.querySelector('small');
  small.innerText = 'message';

//checklength of username and password
function checkLength(username, password){


1 Answer

Hi, you could do that:

const minimumPasswordLength = 4;
function checkLength(username,password){
if(password.length < minimumPasswordLength || username.length < minimumPasswordLength){
return false;
return true;

If it helped you, please mark as best answer!