Reputation: 33
This is my first attempt at making a javascript and I've only started learning about 2 weeks ago, so bare with potential newbishness. Im basically just trying to convert an international number to what needs to be actually dialed.
I need to figure out how to remove a leading zero from the phonenumber variable and no other zeros. Ive seen answers on this that involve parseInt and other solutions but I cant figure out how to implement them.
<script>
var countrycode = document.getElementById("country").value;
var phonenumber = document.getElementById("phnm").value;
function updatecountrycode() // Updates country code on user change //
{countrycode = document.getElementById("country").value;}
function updatenumber() // Updates phone number on user input //
{phonenumber = document.getElementById("phnm").value;}
function fullnumber() // Displays country code + phone number //
document.getElementById("output").innerHTML = 9011 + countrycode + phonenumber};
</script>
<form>
Choose the country you want to call
<select id="country" onchange="updatecountrycode();">
<option>Choose Country</option>
<option value="44" id="uk">United Kingdom</option>
<option value="353" id="ire">Ireland</option>
</select>
<br>
<br>
Enter Phone Number
<input type="text" id="phnm" onchange="updatenumber()"><br>
</form>
<button type="button" onclick="fullnumber()">complete number will display</button>
<p id="output">test</p>
Upvotes: 3
Views: 3118
Reputation: 51
Sorry I know I am late on this question but I think this question is still relevant for some users, so I would like to share a simple function which I have created to remove zero for a text box.
To use this function simply parse your value as an argument in this function i.e removeZero(yourValue)
function removeZero(v) {
if ( v.charAt(0) == 0 ) {
return v.slice(1);
}
}
Upvotes: 0
Reputation: 22875
I created a fiddle for you. https://jsbin.com/dotoha/edit?html,js,output
parseInt solution works fine, because e.g. praseInt('032132323') will produce 32132323
and for multiple leading zeros it will work as well
e.g. parseInt('0003434344434') will give 3434344434
var countrycode = document.getElementById("country").value;
var phonenumber = document.getElementById("phnm").value;
function updatecountrycode() {
// Updates country code on user change //
countrycode = document.getElementById("country").value;
}
function fullnumber() {
// Displays country code + phone number //
var phonenumber = document.getElementById("phnm").value;
document.getElementById("output").innerHTML = 9011 + countrycode + parseInt(phonenumber);
}
Upvotes: 0
Reputation: 3034
JavaScript:
var PhoneNumber = "0545";
var PhoneNumber = parseInt(PhoneNumber, "10");
Problem solved. The second parameter for parseInt (10) is to indicate a decimal based number (default is 8-based). But, parsing your number as an integer will easily remove leading zeros - and you don't have to worry about substrings or if statements.
Upvotes: 0
Reputation: 4163
A simple solution:
if (phonenumber.substr(0,1) == "0")
{
phonenumber = phonenumber.substr(1);
}
Upvotes: 4