Created
November 5, 2019 19:57
-
-
Save brickbones/73da5aa2ee33c2e3597d60c9fc7dc27a to your computer and use it in GitHub Desktop.
Regex Phone Number
This file contains 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 rgx = /^(\(\d{3}\)|\d{3})-?\d{3}-?\d{4}$/g | |
const phoneNumber = document.querySelector('.phone-number') | |
const phoneNumberInput = phoneNumber.querySelector('input') | |
const getNumbers = (event) => { | |
console.log(event) | |
if(rgx.test(event.target.value)){ | |
phoneNumberInput.classList.add('accepted') | |
} else { | |
phoneNumberInput.classList.remove('accepted') | |
} | |
} | |
phoneNumberInput.addEventListener('input', getNumbers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment