Check If a String Contains Only Numbers in Javascript

In this blog post, we learn how to check or validate a string containing only numbers. Often this validation is used to validate the phone number.

1. First Method using RegExp \D

To validate I am using RegExp \D Metacharacter. The \D metacharacter matches non-digit characters. “isNum” variable returns a true or false.

// a string
const str = "14755";

let isNum= /^\d+$/.test(str);

	console.log("A number.");
	console.log("Not a number.");


A number.

2. Second Method using parseInt()

The parseInt() the function parses a string argument and returns an integer of the specified radix.

var value = "78544";

 if(parseInt(value)) {
  console.log(value+" is a number.");
 else {
  console.log(value+" is not a number.");


78544 is a number.