Last active
July 15, 2020 05:42
-
-
Save phfilly/4dd387d2847eb8862fc5784519245753 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | |
// The telephone number must be in the format of (555) 555-5555 | |
function isValidTelephone(telephone) {} | |
// Must be a valid email address | |
function isValidEmail(email) {} | |
/** | |
* | |
* FORMATTING FUNCTIONS | |
* | |
*/ | |
function formatTelephone(text) {} | |
/** | |
* | |
* 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)); | |
emailInput.addEventListener("input", createListener(isValidEmail)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | |
// The telephone number must be in the format of (555) 555-5555 | |
function isValidTelephone(telephone) {} | |
// Must be a valid email address | |
function isValidEmail(email) {} | |
/** | |
* | |
* FORMATTING FUNCTIONS | |
* | |
*/ | |
function formatTelephone(text) {} | |
/** | |
* | |
* 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)); | |
emailInput.addEventListener("input", createListener(isValidEmail)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment