Last active
November 12, 2024 03:11
-
-
Save WebSofter/7d2781fb1b30494aed1fba80af01fe3f to your computer and use it in GitHub Desktop.
Alwais add country code in phone number input with JavaScript
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
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