Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*******************************************************
* onKeyUp(e)
* when a key is pressed up, grab the contents in that
* input field, format them in line with XXX-XXX-XXXX
* format and validate if the text is infact a complete
* phone number. Adjust the border color based on the
* result of that validation
*******************************************************/
function onKeyUp(e){
var input = e.target;
var formatted = formatPhoneText(input.value);
var isError = (validatePhone(formatted) || formatted.length == 0);
var color = (isError) ? "gray" : "red";
var borderWidth = (isError)? "1px" : "3px";
input.style.borderColor = color;
input.style.borderWidth = borderWidth;
input.value = formatted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment