Last active
March 4, 2021 13:49
-
-
Save dkvadratu/627358d6af2de0113ea4b75f2829759c to your computer and use it in GitHub Desktop.
{JS} Input fields validations and replacements, LT phone number
This file contains hidden or 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
//phone format for LT countries | |
var telInput = document.getElementById('billing_phone'); | |
telInput.addEventListener("keyup", function onKeyUp(e){ | |
var input = e.target; | |
input.style.borderColor = 'initial'; | |
//get country and check if its LT | |
if( !isCountry("LT") ) return; | |
//format number | |
var formatted = formatPhoneText(input.value); | |
//validate | |
if( formatted.length >= 12 ) { | |
formatted = formatted.slice(0,12) | |
} | |
if( formatted.length !== 12 || formatted.charAt(0) !== "+" ) { | |
input.style.borderColor = 'red'; | |
} | |
//set new value | |
input.value = formatted; | |
}); | |
//postcode format for LT countries, only numbers | |
var billing_postcode = document.getElementById('billing_postcode'); | |
billing_postcode.addEventListener('change', function () { | |
if( isCountry("LT") ) { | |
billing_postcode.value = billing_postcode.value.trim().replace(/([^0-9]+)/gi, ''); | |
} | |
}) | |
//email fix, once in a while user buys with gmail.con, so convert it to gmail.com | |
var billing_email = document.getElementById('billing_email'); | |
billing_email.addEventListener('change', function () { | |
if( isCountry("LT") ) { | |
billing_email.value = billing_email.value.trim().replace('gmail.con', 'gmail.com'); | |
} | |
}) | |
function formatPhoneText(value){ | |
//convert 86 to +3706 | |
//convert + to +3706 | |
if( value.length < 3 && (value === "86" || value === "+") ) { | |
value = "+3706" | |
} | |
return value.trim().replace(/([^+0-9]+)/gi, ''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment