Home

Remove non-numeric characters from a string - JavaScript

Few days ago, I was trying to validate a mobile number using JavaScript. I wanted to use a regex. This a small snippet that I used to remove all the non-numeric characters from the user input.

str.replace(/\D/g, '');

Normally we will store phone numbers as a string. But even if we are storing it as a number, it is not a good idea to show input field of number type to the user.

There is another input type tel for this use

<input type="tel" />


Last Updated on

Next Post: Rules vs Guidelines →

Comments