Check whether an input string contains a number in javascript

Asked
Active3 hr before
Viewed126 times

7 Answers

javascriptcheckinput
90%

Check if the strings -> “This is 123 online coaching” and “This is gold online coaching” contain numbers.,The isNan() function in javascript determines if the input passed is a number or not. This function returns true if the parameter is Not a Number. Else returns false.,Another way to write and use the function for checking if a javascript string contains a number is:,Check if a string contains numbers in javascript using for loop and isNaN()

Function:-

function stringContainsNumber(_string) {
   return /\d/.test(_string);
}
load more v
88%

9 It does. Check in console: hasNumber("check 3.2 or 1e4") = true vs hasNumber("check no numbers") = false. Because 3.2 and 1e4 contain numbers in itself. – Zon Jan 17 '17 at 18:02 ,Below code checks for same number, sequence number and reverse number sequence.,Update: To check if a string has numbers in them, you can use regular expressions to do that, this solution doesn't take into account non-integer numbers like 3.2 or 1e4 – ekkis Jan 12 '17 at 21:05

If I'm not mistaken, the question requires "contains number", not "is number". So:

function hasNumber(myString) {
   return /\d/.test(myString);
}
load more v
72%

To check if a string contains at least one number using regex, you can use the \d regular expression character class in JavaScript.,At last, we can use the test() method in the regular expression and pass the string as an argument to the method to test if the string contains at least one number. It can be done like this, ,Now let's write the regex by wrapping the \d character class inside regular expression delimiters like this /\d/. ,The \d character class is the simplest way to match numbers.

// Check if string contain atleast one number 🔥
/\d/.test("Hello123World!"); // true
load more v
65%

if (typeof myVar === 'string') {
   //I am indeed a string
}
load more v
75%

I added a new test, indexof, that I thought would be faster than Loop but is actually pretty crappy.,In rev 2 when the input string length is 20, Regex is slightly faster.,In rev 3 when the input string length is 10, Loop is slightly faster.,I'm sure this seems very pedantic, but in programming, is is often far more valuable to be strict about what you want done, than to be strict about how you do it.

var hasNumber = /\d/;
hasNumber.test("ABC"); // false
hasNumber.test("Easy as 123"); // true
load more v
40%

Given a string and the task is to check whether it contains only digits or not.,JavaScript test() Method: This method tests for a pattern in a string. This method returns true if match is found, otherwise it returns false.,How to check if string contains only digits in JavaScript ?,JavaScript string.slice() Method

Syntax:

RegExpObject.test(str)
load more v
22%

My end goal is to validate an input field. The input may be either alphabetic or numeric.,onlyMixOfAlphaNumeric « It checks for string contain both letters and numbers only of any sequence order.,contains_alphaNumeric « It checks for string contains either letter or number (or) both letter and number. The hyphen (-) is ignored.,Update: To check if a string has numbers in them, you can use regular expressions to do that

If I'm not mistaken, the question requires "contains number", not "is number". So:

function hasNumber(myString) {
   return /d/.test(myString);
}
load more v

Other "javascript-check" queries related to "Check whether an input string contains a number in javascript"