1

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

Terminat
  • 1,199
  • 5
  • 21
user2940558
  • 21
  • 1
  • 4
  • Does this answer your question? [How to use Javascript slice to extract first and last letter of a string?](https://stackoverflow.com/questions/19951188/how-to-use-javascript-slice-to-extract-first-and-last-letter-of-a-string) – Eldar B. Sep 17 '21 at 08:37
  • if str[0] == "0" then str = str.substring(1) – Dimitar Sep 17 '21 at 08:37
  • You can call `parseInt()` or `Number()` on the string to turn it into an int, which will remove leading zeros. Or you can use `.replace(/^0+/, '')` –  Sep 17 '21 at 08:42
  • @Chris G: Cant phone numbers start with multiple zeros? Atleast my office number does. – Lain Sep 17 '21 at 08:47
  • @Lain yes, which is why the dupe I linked and the three ways in my comment all remove one or more leading zeros :) –  Sep 17 '21 at 08:52
  • @Chris G: Exactly. Is it not wrong to remove more than one zero? If I try to call my number with all of them removed I get *invalid number*. It works without country code and the zeros or with adding the country code and only removing the first zero. – Lain Sep 17 '21 at 08:53
  • @Lain I see, I misread your comment. I'm reasonably sure that local phone numbers in Kyrgyzstan all have just one leading zero ;) just kidding, no idea, but OP will surely tell us –  Sep 17 '21 at 08:56

4 Answers4

2

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.

Dimitar
  • 1,148
  • 8
  • 29
1

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());
1

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 !

DessM2C
  • 71
  • 1
  • 8
0

If you store the value of the phone number in the html element <input> as a string - like this:

<input type='text' />

Then just use the native javascript parseInt() method to remove the leading zero, which removes the leading zero.

Like this:

const withoutZero = parseInt(input.val());

Or you can leave the value as a string, and if the string starts with "0", you can also delete it as follows:

const withoutZero = input.val().replace(/^0+/, '');
Petr Fořt Fru-Fru
  • 858
  • 2
  • 8
  • 23