Javascript: Check if string contains only digits

While developing something in javascript, we often come across a general requirement to check if a string contains only digits. This article will check if a javascript string has only numbers using different methods and illustrations. Also, this article will cover how to check if a string contains only numbers and decimals including integer, float and double.

Table of Contents:-

Javascript: Check if a string contains only digits using Regex

Regex: the regular expressions are patterns used to match character combinations in strings.

Using match() function

Here in the below solution, we are using the match() function, which returns the result if the string is matching.

Function:-

function checkIfStringHasOnlyDigits(_string)
{
    if(_string.match(/^[0-9]+$/) != null)
    {
        console.log("String contains only numbers")
    }
    else
    {
        console.log("String does not contain only numbers") 
    }
}

Usage:-

checkIfStringHasOnlyDigits("123ThisPointer.com")
checkIfStringHasOnlyDigits("8965")
checkIfStringHasOnlyDigits("89.65")
checkIfStringHasOnlyDigits("")

Output:-

String does not contain only numbers
String contains only numbers
String does not contain only numbers
String does not contain only numbers

The same is achieved using the test() method as well. Let us look into one example.

Using test() function

Here test() function is used, which executes the search to find out if the string and the regular expression are matching or not.

Function:-

function checkIfStringHasOnlyDigits(_string)
{
    if(/^\d+$/.test(_string))
    {
        console.log("String contains only numbers")
    }
    else
    {
        console.log("String does not contain only numbers") 
    }
}

Usage:-

checkIfStringHasOnlyDigits("123ThisPointer.com")
checkIfStringHasOnlyDigits("8965")
checkIfStringHasOnlyDigits("89.65")
checkIfStringHasOnlyDigits("")

Output:-

String does not contain only numbers
String contains only numbers
String does not contain only numbers
String does not contain only numbers

Javascript: Check if a string contains only digits without using Regex

In the below solution, we are using the function charCodeAt(index), which returns an integer representing the UTF-16 code value of the index passed.

Function:-

function checkIfStringHasOnlyDigits(_string) {
    for (let i = _string.length - 1; i >= 0; i--) {
      const codeValue = _string.charCodeAt(i);
      if (codeValue < 48 || codeValue > 57) 
      return false
    }
    return true
  }

Usage:-

checkIfStringHasOnlyDigits("123ThisPointer.com") ? console.log("String contains only numbers ") : console.log("String does not contain only numbers")
checkIfStringHasOnlyDigits("8965") ? console.log("String contains only numbers ") : console.log("String does not contain only numbers")

Output:-

String does not contain only numbers
String contains only numbers 

Javascript: Check if a string contains only numbers and decimal

The below solution will work to check if the string contains only numbers, including float and double.

Function:-

function checkForOnlyNumberDecimal(_string)
{
    if(/^\d+\.\d+$|^\d+$/.test(_string))
    {
        console.log("String contains only numbers and decimal")
    }
    else
    {
        console.log("String does not contain only numbers and decimal") 
    }
}

Usage:-

checkForOnlyNumberDecimal("123ThisPointer.com")
checkForOnlyNumberDecimal("8965")
checkForOnlyNumberDecimal("89.65")
checkForOnlyNumberDecimal("")

Output:-

String does not contain only numbers and decimal
String contains only numbers and decimal
String contains only numbers and decimal
String does not contain only numbers and decimal

Javascript: Check if a string contains only numbers, comma and decimal

Function:-

function checkForOnlyNumberDecimalComma(_string)
{
    if(/^[0-9,.]+$/.test(_string))
    {
        console.log("String contains only numbers, decimal and comma")
    }
    else
    {
        console.log("String does not contain only numbers, decimal and comma") 
    }
}

Usage:-

checkForOnlyNumberDecimalComma("123ThisPointer.com")
checkForOnlyNumberDecimalComma("8965")
checkForOnlyNumberDecimalComma("89.65")
checkForOnlyNumberDecimalComma("89.65,89")
checkForOnlyNumberDecimalComma("")

Output:-

String contains only numbers, decimal and comma
String contains only numbers, decimal and comma
String contains only numbers, decimal and comma
String does not contain only numbers, decimal and comma

Read More:

We hope this article helped you check if a javascript string contains only numbers, including integers, float, and double. Good Luck !!!

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top