We have a lot of phone numbers that contain dashes and start with a zero which are from another country.
I am trying to strip the dashes and the one leading zero and select the country code from a drop-down box.
I'm using "raw" JS with DOM here (for the sake of not having to pull in a framework):
<html>
<head><title>Dash Remover</title></head>
<body>
<form method="POST">
<select id="countryCallingCode">
<option value="0043">+43</option>
<option value="0044">+44</option>
<option value="0049">+49</option>
<option value="">Other</option>
</select>
<input id="numbertodial" type="text" size="16"
oninput="prepareTelNo()" />
<button name="callBtn" onclick="callTelNo();">Call!</button>
</form>
<script>
function getCCC() { return document.getElementById('countryCallingCode').value; }
function prepareTelNo() {
var wd = document.getElementById('numbertodial');
wd.value = wd.value.replace(/-/g, ''); // strip - from the tel. no.
// Cut off leading zeros
if(wd.value.charAt(0) === '0' & getCCC() != "")
wd.value = wd.value.substring(1);
}
function callTelNo() {
var telNo = document.getElementById('numbertodial').value;
window.alert('Calling '+getCCC()+telNo);
}
</script>
</body>
</html>
Any room for improvement? I know that oninput
is only supported in some browsers.
That's ok, it's for an intranet app that is usually used with IE 10.