Skip to content

Instantly share code, notes, and snippets.

@WebSofter
Last active November 12, 2024 03:11
Show Gist options
  • Save WebSofter/7d2781fb1b30494aed1fba80af01fe3f to your computer and use it in GitHub Desktop.
Save WebSofter/7d2781fb1b30494aed1fba80af01fe3f to your computer and use it in GitHub Desktop.
Alwais add country code in phone number input with JavaScript
document.querySelector('.reg_phone').addEventListener('input', (e) => {
const code = '7'
const phone = e.target.value?.replace(/\D+/g, '');
if(phone.indexOf(`+${code}`) !== 0) {
if(phone.indexOf(code) === 0) {
e.target.value = `+${phone}`
} else {
e.target.value = `+${code}${phone.replace('+', '')}`
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment