Validate and Fix Phone Number with JavaScript


Photo by James Sutton on Unsplash

There is a standard for the phone number called ‘E.164’.

It says that the phone number should have a ‘+’ sign at the start and a maximum of 15 digits.

So, our JavaScript should:

  1. Check if there is a ‘+’ sign at the start of the string and add it if necessary.
  2. Do not allow to enter letters and other signs. Only digits.
  3. Allow to enter up to 15 digits.

The JavaScript code which implements this functionality: