Skip to content

Instantly share code, notes, and snippets.

@brickbones
Created November 5, 2019 19:57
Show Gist options
  • Save brickbones/73da5aa2ee33c2e3597d60c9fc7dc27a to your computer and use it in GitHub Desktop.
Save brickbones/73da5aa2ee33c2e3597d60c9fc7dc27a to your computer and use it in GitHub Desktop.
Regex Phone Number
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