JavaScript - How To Check if String Contains Only Digits? Last Updated : 06 Dec, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A string with only digits means it consists solely of numeric characters (0-9) and contains no other characters or symbols. Here are the different methods to check if the string contains only digits.1. Using Regular Expression (RegExp) with test() MethodThe most efficient way to check if a string contains only digits is by using a regular expression with the test() method. This method checks if the string matches the pattern for digits. JavaScript let s = "123456"; let regex = /^\d+$/; let res = regex.test(s); console.log(res); Outputtrue ^ asserts the start of the string.\d+ matches one or more digits.$ asserts the end of the string.test() method returns true if the string contains only digits and false otherwise.2. Using isNaN() MethodThe isNaN() function can be used to check if a value is NaN (Not-a-Number). If a string is entirely composed of digits, it will convert to a valid number, and isNaN() will return false. If it contains anything other than digits, it will return true. JavaScript let s = "123456"; let res = !isNaN(s); console.log(res); Outputtrue isNaN(s) returns true if s is not a number and false if it is a valid number.The ! negates the result to check if the string is a valid number.3. Using Array.prototype.every() with split()You can use the every() method in combination with split() to check if every character in the string is a digit. This method checks each character in the string and ensures all characters are digits. JavaScript let s = "123456"; let res = s.split('').every(char => char >= '0' && char <= '9'); console.log(res); Outputtrue split('') splits the string into an array of individual characters.every() checks that every character in the array is a digit between '0' and '9'.4. Using parseInt() MethodYou can also use the parseInt() function to convert the string to a number and check if the result matches the original string. If the string contains anything other than digits, the conversion will not match. JavaScript let s = "123456"; let res = parseInt(s) == s; console.log(res); Outputtrue parseInt(s) converts the string to an integer.If the conversion result is the same as the origihnal string, it means the string contained only digits.5. Using Number() MethodThe Number() method can also be used to convert a string to a number. If the string contains only digits, it will return a valid number; otherwise, it will return NaN. JavaScript let s = "123456"; let res = !isNaN(Number(s)); console.log(res); Outputtrue Number(s) attempts to convert the string to a number.isNaN() checks if the result is NaN or a valid number.ConclusionRegular Expressions (^\d+$) are the most simple and efficient way to check for digit-only strings.isNaN() and Number() methods are good for general number validation.split() and every() provide a more manual approach but are still effective for checking individual characters.For most use cases, the regular expression method is preferred due to its simplicity and readability. Comment More infoAdvertise with us Next Article JavaScript Program to Check if a Number is Float or Integer P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-RegExp Similar Reads Check if a Given String is Binary String or Not in JavaScript Binary strings are sequences of characters containing only the digits 0 and 1. Other than that no number can be considered as Binary Number. We are going to check whether the given string is Binary or not by checking it's every character present in the string.Example:Input: "101010"Output: True, bin 3 min read JavaScript Program to Check if a Number is Float or Integer In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope 2 min read JavaScript - Strip All Non-Numeric Characters From String Here are the different methods to strip all non-numeric characters from the string.1. Using replace() Method (Most Common)The replace() method with a regular expression is the most popular and efficient way to strip all non-numeric characters from a string.JavaScriptconst s1 = "abc123xyz456"; const 2 min read JavaScript regex - Validate Credit Card in JS To validate a credit card number in JavaScript we will use regular expression combined with Luhn's algorithm. Appling Luhn's algorithm to perform a checksum validation for added securityLuhn algorithm:It first sanitizes the input by removing any non-digit characters (e.g., spaces).It then processes 3 min read JavaScript RegExp D( non-digit characters) Metacharacter The RegExp \D Metacharacter in JavaScript is used to search non-digit characters i.e all the characters except digits. It is the same as [^0-9]. JavaScriptlet str = "a1234g5g5"; let regex = /\D/g; let match = str.match(regex); console.log("Found " + match.length + " matches: " + match);OutputFound 3 1 min read Check if a given String is Binary String or Not using PHP Given a String, the task is to check whether the given string is a binary string or not in PHP. A binary string is a string that should only contain the '0' and '1' characters. Examples:Input: str = "10111001"Output: The string is binary.Input: str = "123456"Output: The string is not binary.Table of 5 min read Like