Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Validate the first field of the form, the username field. This field can only contain letters a-z in lowercase.
Closures in JavaScript
If you want to learn more about closures check out this Treehouse workshop. For more on the specific closure used in this project, read on below.
Creating a Listener
The following function from the project accepts a validator and produces an event listener function. The returned function can then be passed to document.addEventListener()
as a callback function. Below, you can see it's being used to do just that: it creates listeners and supplies them to addEventListener
. The listener it produces accepts an event object, e
, and uses the validator to check whether the input it's attached to is valid or not.
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));
emailInput.addEventListener("input", createListener(isValidEmail));
This function makes the code much more concise than it would be otherwise. To do the same thing without this function, we'd need to write each handler individually, which might look something like this:
usernameInput.addEventListener("input", e => {
const text = e.target.value;
const valid = isValidUsername(text);
const showTip = text !== "" && !valid;
const tooltip = e.target.nextElementSibling;
showOrHideTip(showTip, tooltip);
});
passwordInput.addEventListener("input", e => {
const text = e.target.value;
const valid = isValidPassword(text);
const showTip = text !== "" && !valid;
const tooltip = e.target.nextElementSibling;
showOrHideTip(showTip, tooltip);
});
telephoneInput.addEventListener("input", e => {
const text = e.target.value;
const valid = isValidTelephone(text);
const showTip = text !== "" && !valid;
const tooltip = e.target.nextElementSibling;
showOrHideTip(showTip, tooltip);
});
emailInput.addEventListener("input", e => {
const text = e.target.value;
const valid = isValidEmail(text);
const showTip = text !== "" && !valid;
const tooltip = e.target.nextElementSibling;
showOrHideTip(showTip, tooltip);
});
Notice all the repetition in this second code example. The createListener
function helps clean that up.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up