I have this code and I want to remove first zero from phone number.
mobile = $('#country_code').val();
mobile += input.val();
Country code is: 966
Phone input is: 055642444
And output in this code is: 966055642444
I want it to be 96655642444 without zero after country code.
Thanks
Have you tried parseInt(), integers don't have leading 0s - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
mobile = $('#country_code').val();
mobile += parseInt(input.val());
You can use substring to remove the first N characters from a string.
First of all, assign a variable with the value of input.val()
let str = input.val()
Now, you can index the Nth element of str and check its value:
if (str[0] === "0") str = str.substring(1)
With this, you have successfully checked if the first character is "0" and reassigned the str variable accordingly.
For this, I'd use substring which returns a new string. Example :
mobile = '0652447766';
mobile = mobile.substring(1)
console.log('mobile = ', mobile)
>> mobile = '652447766'
Warning ! Calling mobile.substring() doesn't actually modify mobile ! Strings are immutable in Js, so you'll have to get the returned value of substring() with mobile = mobile.substring(1)
Hope it helped !