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

Why wont the telephone number reformat?

the telephone number is supposed to reformat after the user inputs a 10 digit number, i Have no errors and code matches exscatly but is not firing.

const usernameInput = document.getElementById("username"); const passwordInput = document.getElementById("password"); const telephoneInput = document.getElementById("telephone"); const emailInput = document.getElementById("email");

/**

  • VALIDATORS
  • */

// Can only contain letters a-z in lowercase function isValidUsername(username) { return /^[a-z]+$/.test(username)

}

// Must contain a lowercase, uppercase letter and a number function isValidPassword(password) { return /[a-z]/.test(password)&& /[A-Z]/.test(password)&& /[0-9]/.test(password)

}

// The telephone number must be in the format of (555) 555-5555 (\D* looks for non numeral characters) function isValidTelephone(telephone) { return /^\D*\d{3}\D*\d{3}\D*\d{4}\D*$/.test(telephone)

}

// Must be a valid email address function isValidEmail(email) { return /^[^@]+@[^@.]+.[a-z]+$/i.test(email)

}

/**

  • FORMATTING FUNCTIONS
  • */

function formatTelephone(text) { const regEx = /^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*$/ return text.replace(regEx, '($1) $2-$3')

}

/**

  • SET UP EVENTS
  • */

function showOrHideTip(show, element) { // show element when show is true, hide when false if (show) { element.style.display = "inherit"; } else { element.style.display = "none"; } }

function createListener(validator) { return e => { const text = e.target.value; const valid = validator(text); const showTip = text !== "" && !valid; const tooltip = e.target.nextElementSibling; showOrHideTip(showTip, tooltip); }; }

usernameInput.addEventListener("input", createListener(isValidUsername));

passwordInput.addEventListener("input", createListener(isValidPassword));

telephoneInput.addEventListener("input", createListener(isValidTelephone));

telephoneInput.addEventListener("submit", e =>{ e.target.value = formatTelephone(e.target.value);

});

emailInput.addEventListener("input", createListener(isValidEmail));