10 examples of 'javascript check if string contains only numbers' in JavaScript

Every line of 'javascript check if string contains only numbers' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1function containsNumber(str) {
2 return !!str.match(/\d/g);
3}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

19function isAllDigits(theString) {
20 for (var i = 0; i < theString.length; i++) {
21 var aChar = theString.substring(i, i + 1)
22 if (aChar < "0" || aChar > "9") {
23 return false;
24 }
25 }
26 return true;
27}
15function getNumbers(string) {
16 if (isUndefined(string) || !isString(string)) {
17 return '';
18 }
19
20 return String(string).replace(/\D+/g, '');
21}
62function isNumber(value) {
63 var patrn = /^(-)?\d+(\.\d+)?$/;
64 if (patrn.exec(value) == null || value == "") {
65 return false
66 } else {
67 return true
68 }
69}
35function isValidNumber(value) {
36 const regex = new RegExp('^[-]?[0-9]*$');
37 const result = regex.test(value);
38
39 return result;
40}
12protected static hasNumber(value: string): boolean {
13 return !(/\d/.test(value));
14}
41function isNumber(str) {
42 return /^[-]?\d+(\.\d*)$/.test(str);
43}
8function validateNumber(num) {
9 num = (num + '').replace(/\s+|-/g, '');
10
11 if (!/^\d+$/.test(num)) {
12 return false;
13 }
14
15 var card = cardFromNumber(num);
16
17 if (card) {
18 var cardNumbers = A(card.length);
19
20 return ( cardNumbers.includes(num.length)) && (card.luhn === false || luhnCheck(num));
21 }
22
23 return false;
24}
135function validateNum(value){
136 if(/^\d{4,11}$/.test(value)){
137 return true;
138 }else{
139 return false;
140 }
141}
33hasNumber(myString) {
34 return /\d/.test(myString);
35 }

Related snippets